Python Forum
Fequency of letters with dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fequency of letters with dictionary
#1
Hello guys! So I have been trying to fix this but I havent made it yet, so if someone knows please give me a hand!

so the instructions are this:

" Enter a text and count the number of occurrences of letters and punctuation symbols (except spaces) to form a dictionary of "Number-Character List". That is, the key of the dictionary is the number of occurrences, and the value is a list of letters and punctuation symbols with the same occurrence times."

For example, the dictionary corresponding to "apple" is {1:['a','l','e'], and 2:['p']}.

right now I have done this:
test_str=input('Enter some text:' )
res={}
for keys in test_str:
    res[keys]=res.get(keys,0)+1
print('The "Freq-chars" dictionary is: \n' + str(res))
and this is my output:
Output:
Enter some text:saudi nuclear program accelerates, raising tensions in a volatile region The "Freq-chars" dictionary is: {'s': 5, 'a': 8, 'u': 2, 'd': 1, 'i': 7, ' ': 9, 'n': 6, 'c': 3, 'l': 4, 'e': 7, 'r': 6, 'p': 1, 'o': 4, 'g': 3, 'm': 1, 't': 3, ',': 1, 'v': 1}
but what the output should look like is:
[Image: download?verifier=Sm16TMkWU5HBqjEOcoe1Y2...IBq&wrap=1]
the thing here is that mine right now is (letter:times) but I should have (time,letter) for example: 1:['a','t','c'] instead of 'a':1 , 't':1, etc.

Thank you so much for your help and comments! Heart
Reply
#2
now you have a dictionary with a count of letters.
create another empty dictionary.
create a for loop for key(old), value(old) of the dictionary you created
if the value(old) is a key in the new dictionary
append the key(old) to the key(new)
else
set the key(new) to a list with the value(old) inside.

it makes sense to me but might not to you basically you are flipping the key, & values between dictionary's and storing the values in a list.
Reply
#3
Some random ideas.

Task is to flip keys and values with possibility that there are several equal values. As dictionary keys are unique one can't directly flip them - Python will overwrite key-value pairs and keep only last one:

>>> d = {'ham': 3, 'spam': 4, 'bacon': 5, 'eggs': 4}
>>> {v:k for k, v in d.items()}
{3: 'ham', 4: 'eggs', 5: 'bacon'}
So the type of the value should be list. Another thing which should be taken care is problem of missing keys. In most basic form it can be solved as:

>>> d = {'ham': 3, 'spam': 4, 'bacon': 5, 'eggs': 4}
>>> new = {}
>>> for k, v in d.items():         # iterate over original dict key-value pairs
...     if v not in new:           # if v is not among new dict keys
...         new[v] = []            # then create key in new dict with value of empty list
...     new[v].append(k)           # append key as value to list
...
>>> new
{3: ['ham'], 4: ['spam', 'eggs'], 5: ['bacon']}
There are also more 'advanced' ways achieving same result. Like using dictionary get method as in counting letters:

>>> d = {'ham': 3, 'spam': 4, 'bacon': 5, 'eggs': 4}
>>> new = {}
>>> for k, v in d.items():
...     new[v] = new.get(v, []) + [k]
...
>>> new
{3: ['ham'], 4: ['spam', 'eggs'], 5: ['bacon']}
Flipping keys and values can be done also using defaultdict, if needed it can be converted to regular dict:

>>> from collections import defaultdict
>>> d = {'ham': 3, 'spam': 4, 'bacon': 5, 'eggs': 4}
>>> new = defaultdict(list)
>>> for k in d:
...    new[d[k]].append(k)
...
>>> new
defaultdict(<class 'list'>, {3: ['ham'], 4: ['spam', 'eggs'], 5: ['bacon']})
>>> dict(new)
{3: ['ham'], 4: ['spam', 'eggs'], 5: ['bacon']}
However, maybe instead of flipping keys and values create dictionary needed right away? One way of doing it:

>>> s = 'ham spam eggs bacon'
>>> set(s)
{'g', 'h', 'a', 'm', 'p', 'e', 'n', 'c', ' ', 'b', 's', 'o'} # unique symbols in s, incl space
>>> chars = set(s) - set(' ')                                # get rid of space, if space must be counted, keep it
>>> chars
{'g', 'h', 'a', 'm', 'p', 'e', 'n', 'c', 'b', 's', 'o'}
>>> new = dict()
>>> for char in chars:
...     new[s.count(char)] = new.get(s.count(char), []) + [char]
...
>>> new
{2: ['g', 'm', 's'], 1: ['h', 'p', 'e', 'n', 'c', 'b', 'o'], 3: ['a']}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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