I want to create binary values for words based on their content of vowels and consonants, where vowels receive a value of '0' and consonants get a value of '1'.
For example, 'haha' would be represented as 1010, hahaha as 101010.
common_words = ['haha', 'hahaha', 'aardvark', etc...]
dictify = {}
binary_value = []
#doesn't work
for word in common_words:
for x in word:
if x=='a' or x=='e' or x=='i' or x=='o' or x=='u':
binary_value.append(0)
dictify[word]=binary_value
else:
binary_value.append(1)
dictify[word]=binary_value
-With this I am getting too many binary digits in the resulting dictionary:
>>>dictify
{'aardvark': [0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1,...}
desired output:
>>>dictify
{'haha': 1010,'hahaha': 101010, 'aardvark': 00111011}
I am thinking of a solution that doesn't involve a loop within a loop...
each
ornumber_value
come from?dictify = {w:"".join('0' if c in 'aeiouAEIOU' else '1' for c in w) for w in common_words}
00111011
won't work as an integer because there's no way to preserve the initial zeroes. You could use a string or a list.each
andbinary_value
are never set.