Boo*_*d16 4 python string counter list
我试图找到一个字符串列表中的整个单词的数量,继承人的列表
mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point blanchardstown"]
Run Code Online (Sandbox Code Playgroud)
预期结果:
4
1
2
3
Run Code Online (Sandbox Code Playgroud)
mylist [0]中有4个单词,mylist [1]中有1个单词,依此类推
for x, word in enumerate(mylist):
for i, subwords in enumerate(word):
print i
Run Code Online (Sandbox Code Playgroud)
完全不起作用....
你们有什么感想?
Ash*_*ary 18
用途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
Run Code Online (Sandbox Code Playgroud)
最简单的方法应该是
num_words = [len(sentence.split()) for sentence in mylist]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48965 次 |
| 最近记录: |