我有一个密码像:
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是打招呼的时间,2是打招呼的时间,4是婴儿。
Tags:
结果
另外:
您只需使用slist中的
count
方法。我想你可以用口述来更好地控制
现在,如果需要值列表,请执行以下操作:
finallist = finaldict.values()
collections.Counter
显然是您的朋友(除非您需要按特定顺序排序的输出)。把它和生成器理解结合起来,生成所有长度为4的单词,你就是黄金。如果需要按元素的第一次出现顺序排列元素,请使用有序字典:
相关问题 更多 >
编程相关推荐