[英]Counting number of strings in a List with Python
我手上有一个清单,我想从该清单中创建词汇表。 然后,我想显示每个单词并在此列表中计算相同的字符串。
示例列表如下。
new_list = ['one', 'thus', 'once', 'one', 'count', 'once', 'this', 'thus']
首先,我用以下内容创建了一个词汇表。
vocabulary = []
for i in range (0, len(new_list)):
if new_list[i] not in vocabulary:
vocabulary.append(new_list[i])`
print vocabulary
上面代码的输出是:“因此,一次,一次,一次,这个。”
我想在列表中显示每个单词的数量,如下所示。 [count] [1],[once] [2],[one] [2],[this] [1],[thus] [2]。
为了获得以上结果; 我尝试下面的代码。
matris = []
for i in range(0,len(new_list)):
temp = []
temp.insert(0,new_list.count(new_list[i]))
matris.append(temp)
for x in matris:
print x
上面的代码仅给出字数。 有人可以建议我如何一起打印单词名称和单词编号,例如[once] [2]格式。
使用Counter dict获取字数,然后在.items
进行迭代:
from collections import Counter
new_list = ['one', 'thus', 'once', 'one', 'count', 'once', 'this', 'thus']
cn = Counter(new_list)
for k,v in cn.items():
print("{} appears {} time(s)".format(k,v))
如果您想要特定的输出,则可以将元素包装在str.format中:
for k,v in cn.items():
print("[{}][{}]".format(k,v))
[thus][2]
[count][1]
[one][2]
[once][2]
[this][1]
要使输出从最高计数到最低计数,请使用.most_common:
cn = Counter(new_list)
for k,v in cn.most_common():
print("[{}][{}]".format(k,v))
输出:
[once][2]
[thus][2]
[one][2]
[count][1]
[this][1]
如果您希望数据按字母顺序从最低到最高,从最高到最低按字母顺序排列,则需要传递键-x[1]
进行排序,以使计数取反,从而将计数从最高到最低排序:
for k, v in sorted(cn.items(), key=lambda x: (-x[1],x[0])):
print("[{}][{}]".format(k, v))
输出:
[once][2]
[one][2]
[thus][2]
[count][1]
[this][1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.