用Python计算列表中的字符串数

用Python计算列表中的字符串数,python,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

我手上有一个列表,我想从这个列表中创建词汇表。然后,我想显示每个单词并计算列表中的相同字符串

示例列表如下所示

    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,one,one,this,this。”

我想显示列表中每个单词的编号,如下所示。[计数][1],[一次][2],[一次][2],[这个][1],[这样][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

上面的代码只给出了字数。有人能告诉我如何将单词名称和单词编号打印在一起,如[一次][2]格式

使用计数器dict获取字数,然后只需迭代
。项

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]
从最高计数到最低使用的输出。最常见:

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]
[once][2]
[one][2]
[thus][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]
[thus][2]
[one][2]
[count][1]
[this][1]
[once][2]
[one][2]
[thus][2]
[count][1]
[this][1]

使用计数器dict获取字数,然后只需迭代
。项

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]
从最高计数到最低使用的输出。最常见:

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]
[once][2]
[one][2]
[thus][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]
[thus][2]
[one][2]
[count][1]
[this][1]
[once][2]
[one][2]
[thus][2]
[count][1]
[this][1]

谢谢你,帕德雷克。它工作得很好。我还想按数量从maksimum到最小值对单词进行排序。首先,根据计数的数量,然后是字母顺序。例如[一次][2],[一次][2],[因此][2],[计数][1],[这个][1]。参见维基:特别是关于关键功能的部分。首先按单词排序,然后按值排序,以获得所需内容。另外,请注意,根据这个问题,排序在Python中是稳定的:@Padraic它不按字母顺序排序。“对于排序中的k,v(cn.items(),key=lambda x:-x[1]):”它仍然只根据计数的数量进行排序。谢谢Padraic。它工作得很好。我还想按数量从maksimum到最小值对单词进行排序。首先,根据计数的数量,然后是字母顺序。例如[一次][2],[一次][2],[因此][2],[计数][1],[这个][1]。参见维基:特别是关于关键功能的部分。首先按单词排序,然后按值排序,以获得所需内容。另外,请注意,根据这个问题,排序在Python中是稳定的:@Padraic它不按字母顺序排序。“对于k,v,在排序(cn.items(),key=lambda x:-x[1]):”它仍然只根据计数的数量进行排序。