在列表中统计单词出现次数 PYTHON
我有一段代码:
s = "hello this is hello this is baby baby baby baby hello"
slist = s.split()
finallist = []
for word in slist:
if len(word) >= 4:
final = final + [word]
简单来说,上面的代码是用来从一个列表中筛选出那些字符数超过4个的单词。
接下来,我想统计这些单词出现的次数,并把结果保存到一个新的列表里。比如,结果会是[3,2,4],其中3表示“hello”出现了3次,2表示“this”出现了2次,4表示“baby”出现了4次。
3 个回答
1
你只需要使用slist里的count
方法就可以了。
我觉得你可以用一个字典,这样可以更好地管理你的数据。
s = "hello this is hello this is baby baby baby baby hello"
slist = s.split()
finaldict = {}
for word in slist:
if len(word) >= 4 and not finaldict.get(word):
finaldict[word] = slist.count(word)
现在,如果你想要获取所有的值,只需这样做:finallist = finaldict.values()
4
collections.Counter
绝对是你的好帮手(除非你需要输出结果按特定顺序排列)。把它和一个 生成器表达式 结合起来,就可以生成所有长度为4的单词,这样你就成功了。
from collections import Counter
Counter(w for w in s.split() if len(w) >= 4)
如果你需要按元素第一次出现的顺序来排列,可以使用有序字典:
from collections import OrderedDict
wc = OrderedDict()
for w in s.split():
if len(w) >= 4:
wc[w] = wc.get(w, 0) + 1
4
from collections import Counter
import re
reg = re.compile('\S{4,}')
s = "hello this is hello this is baby baby baby baby hello"
c = Counter(ma.group() for ma in reg.finditer(s))
print c
结果
Counter({'baby': 4, 'hello': 3, 'this': 2})
还有:
from collections import defaultdict
d = defaultdict(int)
s = "hello this is hello this is baby baby baby baby hello"
for w in s.split():
if len(w)>=4:
d[w] += 1
print d