Reputation: 3
I need to create a list of letter occurrences from a list of words.
my list of words is ['song', 'boom', 'pow']
I want to count the letter occurrences in each word and then put them in a list in alphabetical order. If there isn't a letter in my word list that matches each letter in the alphabet, I still want it to print a 0 in the list.
Instead of typing out a string of 26 characters in the alphabet, I am using string.ascii_lowercase
I'm not really sure how to go about doing this.
I then I need to find out the letter occurrences in each word and which word[s] the letter is shown in.
Then output should look like:
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 4, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
'o' in 'song', 'boom', 'pow'
etc.
Upvotes: 0
Views: 425
Reputation: 500327
The first part can be done like so:
In [12]: seq = ['song', 'boom', 'pow']
In [13]: c = collections.Counter(''.join(seq))
In [14]: [c.get(l, 0) for l in string.ascii_lowercase]
Out[14]: [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 4, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0]
As to the second part, here is a hint:
In [23]: [w for w in seq if 'o' in w]
Out[23]: ['song', 'boom', 'pow']
Upvotes: 4
Reputation: 103824
Another way that seems natural to me:
>>> from string import ascii_lowercase
>>> words= ['song', 'boom', 'pow']
>>> s=''.join(words)
>>> [s.count(c) for c in ascii_lowercase]
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 4, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0]
For the second part, just use a nested loop:
for c in ascii_lowercase:
li=[]
for word in words:
if c in word:
li.append(word)
if li:
print '{} in {}'.format(c, ', '.join(li))
Prints:
b in boom
g in song
m in boom
n in song
o in song, boom, pow
p in pow
s in song
w in pow
Upvotes: 0
Reputation: 180401
from collections import OrderedDict
from string import ascii_lowercase
l = ['song', 'boom', 'pow']
od = OrderedDict(((k,0) for k in ascii_lowercase)) # create dict from alph with initial val of 0
for word in l:
for let in word:
o[let] += 1 # increase value by 1 for each letter in the words
print(od.values()) # ordereddict keeps order so just print values
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 4, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0]
for k in od:
for word in l:
if k in word:
print(k,word)
('b', 'boom')
('g', 'song')
('m', 'boom')
('n', 'song')
('o', 'song')
('o', 'boom')
('o', 'pow')
('p', 'pow')
('s', 'song')
('w', 'pow')
Process finished with exit code 0
Upvotes: 1