Boosted_d16 Boosted_d16 - 2 months ago 7
Python Question

Python - Count number of words in a list strings

Im trying to find the number of whole words in a list of strings, heres the list

mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point blanchardstown"]


expected outcome:

4
1
2
3


There are 4 words in mylist[0], 1 in mylist[1] and so on

for x, word in enumerate(mylist):
for i, subwords in enumerate(word):
print i


Totally doesnt work....

What do you guys think?

Answer

Use str.split:

>>> mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point  blanchardstown"] 
>>> for item in mylist:
...     print len(item.split())
...     
4
1
2
3