Python - 计算列表字符串中的单词数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18827198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:57:53  来源:igfitidea点击:

Python - Count number of words in a list strings

pythonstringlistcounter

提问by Boosted_d16

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

mylist[0]中有4个单词,mylist[1]中有1个单词,依此类推

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

Totally doesnt work....

完全行不通....

What do you guys think?

你们有什么感想?

采纳答案by Ashwini Chaudhary

Use str.split:

使用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

回答by Hari Menon

The simplest way should be

最简单的方法应该是

num_words = [len(sentence.split()) for sentence in mylist]

回答by Srinivasreddy Jakkireddy

for x,word in enumerate(mylist):
    print len(word.split())

回答by Franck Dernoncourt

You can use NLTK:

您可以使用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]

回答by Usama Chitapure

a="hello world aa aa aa abcd  hello double int float float hello"
words=a.split(" ")
words
dic={}
for word in words:
    if dic.has_key(word):
        dic[word]=dic[word]+1
    else:
        dic[word]=1
dic

回答by Mahesh Acharya

We can count the number of a word's ocurrence in a list using the Counterfunction.

我们可以使用该Counter函数计算一个单词在列表中出现的次数。

from collection import Counter

string = ["mahesh","hello","nepal","nikesh","mahesh","nikesh"]

count_each_word = Counter(string)
print(count_each_word)

Output:

输出:

Counter({mahesh:2},{hello:1},{nepal:1},{nikesh:2})

计数器({mahesh:2},{hello:1},{nepal:1},{nikesh:2})

回答by neosergio

This is another solution:

这是另一种解决方案:

You can clean your data first and then count the result, something like that:

您可以先清理数据,然后计算结果,如下所示:

mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point  blanchardstown"] 
for item in mylist:
    for char in "-.,":
        item = item.replace(char, '')
        item_word_list = item.split()
    print(len(item_word_list))

The result:

结果:

4
1
2
3

回答by sshivanshu992

mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point blanchardstown"]
flage = True
for string1 in mylist:
    n = 0
    for s in range(len(string1)):
        if string1[s] == ' ' and flage == False:
            n+=1
        if string1[s] == ' ':
            flage = True
        else:
            flage = False
    print(n+1)