So, trying to create a function that takes a string input, and with that input, it counts all the instances of all characters to a dictionary (like {'a':5, 'b':4 etc..}
, and then ultimately creates a new dictionary, that consists of 3 keys: vowels, consonants and others, and adds tuples of character:counts in a set to the correct key.
For example: input 'brown fox'
.
Return value should be: {'Vowels':{('o', 2)}, 'Consonants':{('b', 1), ('r', 1), ('w', 1), ('n', 1), ('f', 1), ('x', 1)}, 'Other':{' ', 1}}
.
Currently I thought it seemed simple through the iteration of keys, but ran to a dead end.
My idea is that I use my function symbol_freq to sort all the unique chars to a dictionary, with the character being the key and value the count. Then I would create a empty dictionary, and start iterating through all of the keys there, seeing if they are in the list of vowels and consonants and assigning tuples of said character to the right key in my new dictionary with 3 keys.
Getting the error of:
Traceback (most recent call last): File "....py", line 26, in print(group("tao pihku ajuhälvik"))
File "....py", line 13, in group for key, value in symbol_freq_dict: ValueError: not enough values to unpack (expected 2, got 1)
def symbol_freq(string):
symbol_freq_dict = dict.fromkeys(string, 0)
for i in string:
symbol_freq_dict[i] += 1
return symbol_freq_dict
def group(string):
vowels = ['a', 'e', 'i', 'o', 'u', 'õ', 'ä', 'ö', 'ü']
consonants = ['b', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 'š', 'z', 'ž', 't', 'v']
string.lower()
symbol_freq_dict = symbol_freq(string)
grouped_dict = dict()
for key, value in symbol_freq_dict:
if key in vowels:
grouped_dict['Vowels'].append((key, value))
if key in consonants:
grouped_dict['Consonants'].append((key, value))
else:
grouped_dict['Other'].append((key, value))
return grouped_dict