How do I input I change the letters in a string to numbers for a frequency count?
<p>In preparation for an upcoming national cipher challenge, I was hoping to create a piece of code that would take the coded message as a string and record the frequency of each letter in order to try and figure out what the letter is most likely to be when decoded (I'm sorry if that's not very clear!). This is how I planned to do this:</p>

<hr>

<p>1) Copy + paste the code into a python program</p>

<p>2) The program would convert the letters to numbers</p>

<p>3) for x in range(0,len(<em>name of variable the string is assigned to</em>):
if string[x] in "1":</p>

<p>increase count for '1' by one</p>

<p>x = x + 1</p>

<p>else:</p>

<p>x = x + 1</p>

<p>Then change it to </p>

<p>if string[x] in "2"</p>

<p>and so on until 26 when the program would print the frequency of each number. Any help whatsoever with this code would be appreciated. Even just some pseudocode would massively help.</p>

<p>Thank you!</p>

<p>Matt.</p>

<p>(PS, I'm completely new to this site!)</p>

<p>Your post leads me to believe that you're an absolute beginner, so I think that it would be valuable for you to implement this on your own, so that you understand what's going on.  </p>

<p>First: reading the cipher.</p>

<p>Instead of editing your code every time, you can read the contents of a file.  Copy the cipher into a new txt file.  I'll name mine "cipher.txt"</p>

<p>Then:</p>

<pre><code>f = open('cipher.txt', 'r')
</code></pre>

<p>creates a file object named <code>f</code> that I can use to get the contents of a file. I will put all the text in the file onto one line, by removing the newline characters.</p>

<pre><code>contents = r.read().replace('\n', '')
</code></pre>

<p>So <code>contents</code> is a string that holds all the text in our cipher.  One of the nice things about strings in python is that they are iterable, so we can say</p>

<pre><code>for char in contents:
</code></pre>

<p>to look at all the characters individually, in order.</p>

<p>But hold up!  Where are we going to store our results? Let's assume that there could be any kind of character in our file, even one we may not expect. The other solution returned a list of tuples, but I like to use dictionaries for this kind of thing.</p>

<p>So let's make a dictionary</p>

<pre><code>char_dict = {}
</code></pre>

<p>and start counting!</p>

<pre><code>for char in contents:
    if char not in char_dict:
        char_dict[char] = 1
    else:
        char_dict[char] += 1
</code></pre>

<p>Dictionaries are just pairs of things.  So if a char is not in our dictionary, we pair it with one.  If it is in our dictionary, we increase the number we pair it with by one.</p>

<p>Then we can do things with the dictionary.  To access 'a' in the dictionary you would just say</p>

<pre><code>char_dict['a']
</code></pre>

<p>The other posters answer may be easier to write, but I hope this helped you understand a little bit of what's happening "under the hood", so to speak.</p>

python string