Python-计算列表字符串中的单词数 [英] Python - Count number of words in a list strings
本文介绍了Python-计算列表字符串中的单词数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在字符串列表(这里是列表)中查找整个单词的数量
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"]
预期结果:
4
1
2
3
mylist [0]中有4个单词,mylist [1]中有1个单词,依此类推
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
完全无效....
你们怎么看?
推荐答案
使用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
这篇关于Python-计算列表字符串中的单词数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文