find the numerical index of a list of characters in ascending alphabetical order
<p>I am writing a cryptography program that does columnar transposition. 
a person enters a key as a string, eg, key = 'ZEBRAS'
I need to determine the numerical index corresponding to each letter, in ascending alphabetical order. 
for example, </p>

<ul>
<li><strong>Z E B R A S</strong></li>
<li><strong>6 3 2 4 1 5</strong></li>
</ul>

<p>A is the highest, so its rank 1. z is the lowest, so its rank 6. 
I want to store this value into an appropriate data structure, so when i go to encrypt a message, i will read off the column corresponding to position 1 first, and 6 last. </p>

<p>Create a dictionary from sorted &amp; unique group of letters &amp; indices from 1 to length of the string (you need unicity or several indexes will be generated if there are several occurrences of a letters (as shown below, I have added an S to the word):</p>

<pre><code>s="ZEBRASS"
us=set(s)
sl=dict(zip(sorted(us),range(1,len(us)+1)))
print(sl)
</code></pre>

<p><code>sl</code> contains:</p>

<pre><code>{'Z': 6, 'A': 1, 'E': 3, 'R': 4, 'S': 5, 'B': 2}
</code></pre>

<p>To "encrypt", apply the dictionary to your string:</p>

<pre><code>sc = [sl[c] for c in s]
print(sc)
</code></pre>

<p>result:</p>

<pre><code>[6, 3, 2, 4, 1, 5, 5]
</code></pre>

python sorting cryptography