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>You can try <code>Counter</code> for counting frequency:</p>

<pre><code>from collections import Counter
s = 'abahvhavsvgs'
print(Counter(s))
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>Counter({'v': 3, 'a': 3, 'h': 2, 's': 2, 'b': 1, 'g': 1})
</code></pre>

<p>For iterating over Counter (i.e. accessing particular element) you can use <code>most_common()</code> like:</p>

<pre><code>c =Counter(s)
print(c.most_common())
</code></pre>

<p>Output: (list of tuples)</p>

<pre><code>[('v', 3), ('a', 3), ('s', 2), ('h', 2), ('g', 1), ('b', 1)]
</code></pre>

<blockquote>
  <p><strong>Note:</strong> If you want to see occurrence of particular letter let's say <code>'a'</code>, you can directly access it like <code>print(c['a'])</code> it gives output
  as <code>3</code> and if letter is not present in the string like <code>'z'</code> if you
  execute <code>print(c['z'])</code> it gives output as <code>0</code></p>
</blockquote>

<p>See also the <a href="https://docs.python.org/3/library/collections.html#collections.Counter.most_common" rel="nofollow">documentation of <code>Counter.most_common()</code></a>.So you can <em>iterate</em> over like if you do <code>print(c.most_common()[0])</code> it will give output <code>('v', 3)</code> as character with number of times it is occurred in the string.</p>

<p>For details about <code>collections</code> and <code>counter</code> see this <a href="https://docs.python.org/2/library/collections.html#collections.Counter" rel="nofollow">Counter Explained</a>.</p>

<p><strong>Another Solution</strong>(using <code>defaultdict</code> ):</p>

<pre><code>from collections import defaultdict
s = 'abahvhavsvgs'
d_int = defaultdict(int)    #  to make default value to 0 
for i in s:
    d_int[i] +=1
print(d_int)
</code></pre>

<p>Output:</p>

<pre><code>defaultdict(&lt;class 'int'&gt;, {'v': 3, 'h': 2, 's': 2, 'b': 1, 'g': 1, 'a': 3})
</code></pre>

<p>You can convert it into dictionary by <code>print(dict(d_int))</code> gives you output as <code>{'g': 1, 's': 2, 'v': 3, 'h': 2, 'a': 3, 'b': 1}</code> . Instead of using normal dictionary where every time you have to check if key is present in the dictionary (like in case of @Patrick Haugh solution) you can use <code>defaultdict</code>. For details about defaultdict you can this article <a href="https://docs.python.org/2/library/collections.html#collections.defaultdict" rel="nofollow">defaultdict Document</a>. (<strong>Note:</strong> But then you have to do additional logic to sort by value or to find which letter occurs most times or least times!).   </p>

<p>Hope this helps.</p>

python string