Python Forum
Count occurences in a string and add to a list using loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count occurences in a string and add to a list using loops
#1
Good morning,

May I ask for some help with this homework please?:

Quote:We're given a string called text:

text=""" \
I want a hero: an uncommon want,
When every year and month sends forth a new one,
Till, after cloying the gazettes with cant,
The age discovers he is not the true one;
Of such as these I should not care to vaunt,
I’ll therefore take our ancient friend Don Juan—
We all have seen him, in the pantomime,
Sent to the devil somewhat ere his time """
We want to perform statistics by counting the number of occurrences of each letter and signs.
For this, we can use a dictionary, with the characters encountered as keys and the number of times each character is encountered as value.

We should test our code with:
 print('count of "a"\t:', dict.get('a', 0)) 

My original idea was to create two lists: one with letters, the other with the number of occurence of each caracters in text.
Then to iterate on them to fill the dictionnary, called dict{}.
For the first one, I simply created a list called keys[] and added letters and signs manually (is there a better way to this?):

keys=['A','B','C','D','E','F','a','b','c','d','f','g']
I struggle to fill the second one: I'd like to count each caracter first (potentially with a loop) then append them to a list with the .append method.

to count a caracter and add it the list called values I wrote:
counta=(sum(char == 'a' for char in text)) #define variabe counta to add occurences of 'a' to this variable
values.append(counta)                                 #append to the list values
Doing this for all 26 letters of the alphabet, plus their capitals and the signs.. I have the feeling I should be using a loop, with an if condition. So I tried this:

count=0
for i in text:
    if i=='a'
    count=count+1
    keys.append(count) 
 if i=='b'
    count=count+1
    keys.append(count)  #but since the first condition is filled,   if i=='b' doesn't work 
So in brief, my question is :is there a way to fill the values list with the number of occurrences of each letter in the text, with a loop without writing 52 times the same code.

More than the solution, I would like to understand your thinking behind it.
And please do feel free to point out my errors in the way I translated the problem, so I stop making these mistakes.

thanks,
Leyo
Reply
#2
using a dictionary, you can do it on the fly:
text=""" \
I want a hero: an uncommon want,
When every year and month sends forth a new one,
Till, after cloying the gazettes with cant,
The age discovers he is not the true one;
Of such as these I should not care to vaunt,
I’ll therefore take our ancient friend Don Juan—
We all have seen him, in the pantomime,
Sent to the devil somewhat ere his time """

import string

letter_counts = {}
for letter in string.ascii_uppercase:
    letter_counts[letter] = text.count(letter)
    letter_counts[letter] += text.count(letter.lower())
    print(f"letter {letter.lower()},{letter} count: {letter_counts[letter]}")
Output:
letter a,A count: 21 letter b,B count: 0 letter c,C count: 7 letter d,D count: 7 letter e,E count: 41 letter f,F count: 5 letter g,G count: 3 letter h,H count: 19 letter i,I count: 16 letter j,J count: 1 letter k,K count: 1 letter l,L count: 9 letter m,M count: 8 letter n,N count: 26 letter o,O count: 20 letter p,P count: 1 letter q,Q count: 0 letter r,R count: 13 letter s,S count: 14 letter t,T count: 29 letter u,U count: 7 letter v,V count: 5 letter w,W count: 7 letter x,X count: 0 letter y,Y count: 3 letter z,Z count: 1
Leyo likes this post
Reply
#3
The easiest solution first:

from collections import Counter


text=""" \
I want a hero: an uncommon want,
When every year and month sends forth a new one,
Till, after cloying the gazettes with cant,
The age discovers he is not the true one;
Of such as these I should not care to vaunt,
I’ll therefore take our ancient friend Don Juan—
We all have seen him, in the pantomime,
Sent to the devil somewhat ere his time """


result = Counter(text).most_common()
# without most_common you get the order of first occourence
# with the use of most_common, the output is sorted from small to big occurrence
# if you call most_common with an integer, you get for example the top10 results. 

print(result)
Output:
[(' ', 61), ('e', 41), ('t', 27), ('n', 26), ('a', 21), ('h', 19), ('o', 19), ('r', 13), ('s', 13), ('i', 13), ('l', 9), ('m', 8), ('u', 7), ('c', 7), (',', 7), ('\n', 7), ('d', 6), ('w', 5), ('v', 5), ('f', 5), ('I', 3), ('y', 3), ('g', 3), ('W', 2), ('T', 2), (':', 1), ('z', 1), (';', 1), ('O', 1), ('’', 1), ('k', 1), ('D', 1), ('J', 1), ('—', 1), ('p', 1), ('S', 1)]
What teachers may want to see (no use of library):
text=""" \
I want a hero: an uncommon want,
When every year and month sends forth a new one,
Till, after cloying the gazettes with cant,
The age discovers he is not the true one;
Of such as these I should not care to vaunt,
I’ll therefore take our ancient friend Don Juan—
We all have seen him, in the pantomime,
Sent to the devil somewhat ere his time """


results = {}
for char in text:
    if char not in results:
        # character is not in the dict, so it's the first time
        # that we see this character
        results[char] = 1
    else:
        # the character was already seen in the text, so count it up by 1
        results[char] += 1

print(results)
And a solution with the use of the Python-stdlib before the class Count was introduces (long long time ago)...
from collections import defaultdict

text=""" \
I want a hero: an uncommon want,
When every year and month sends forth a new one,
Till, after cloying the gazettes with cant,
The age discovers he is not the true one;
Of such as these I should not care to vaunt,
I’ll therefore take our ancient friend Don Juan—
We all have seen him, in the pantomime,
Sent to the devil somewhat ere his time """


results = defaultdict(int)
# if a key does not exist, int() will return, which is 0


for char in text:
    results[char] += 1

print(results)
Leyo likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
(Mar-11-2022, 12:14 PM)Larz60+ Wrote: using a dictionary, you can do it on the fly:
text=""" \
I want a hero: an uncommon want,
When every year and month sends forth a new one,
Till, after cloying the gazettes with cant,
The age discovers he is not the true one;
Of such as these I should not care to vaunt,
I’ll therefore take our ancient friend Don Juan—
We all have seen him, in the pantomime,
Sent to the devil somewhat ere his time """

import string

letter_counts = {}
for letter in string.ascii_uppercase:
    letter_counts[letter] = text.count(letter)
    letter_counts[letter] += text.count(letter.lower())
    print(f"letter {letter.lower()},{letter} count: {letter_counts[letter]}")
Output:
letter a,A count: 21 letter b,B count: 0 letter c,C count: 7 letter d,D count: 7 letter e,E count: 41 letter f,F count: 5 letter g,G count: 3 letter h,H count: 19 letter i,I count: 16 letter j,J count: 1 letter k,K count: 1 letter l,L count: 9 letter m,M count: 8 letter n,N count: 26 letter o,O count: 20 letter p,P count: 1 letter q,Q count: 0 letter r,R count: 13 letter s,S count: 14 letter t,T count: 29 letter u,U count: 7 letter v,V count: 5 letter w,W count: 7 letter x,X count: 0 letter y,Y count: 3 letter z,Z count: 1

super useful, thank you!!
Reply
#5
not one but three solutions :) amazing, and thanks for the detailed comments as well, it's super helpful
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,433 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Count Letters in a Sentence (without using the Count Function) bhill 3 5,196 Jun-19-2018, 02:52 AM
Last Post: bhill
  regex, counting occurences yanhto 4 3,381 May-06-2018, 10:20 PM
Last Post: killerrex
  Replacing string values with count tomhuang 2 3,421 Jul-19-2017, 06:38 PM
Last Post: nilamo
  Quick Lists Question (Count Occurences) EwH006 12 10,867 Nov-17-2016, 11:59 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020