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?
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
The simplest way should be
num_words = [len(sentence.split()) for sentence in mylist]
You can use NLTK:
import nltk
mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point blanchardstown"]
print(map(len, map(nltk.word_tokenize, mylist)))
Output:
[4, 1, 2, 3]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With