简体   繁体   English

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

[英]Counting number of strings in a List with Python

I have a list in my hand and I want to create vocabulary from this list. 我手上有一个清单,我想从该清单中创建词汇表。 Then, I want to show each word and count the same strings in this list. 然后,我想显示每个单词并在此列表中计算相同的字符串。

The sample list as below. 示例列表如下。

    new_list = ['one', 'thus', 'once', 'one', 'count', 'once', 'this', 'thus']

First, I created a vocabulary with below. 首先,我用以下内容创建了一个词汇表。

    vocabulary = []
        for i in range (0, len(new_list)):
            if new_list[i] not in vocabulary:
                vocabulary.append(new_list[i])`
    print vocabulary

The output of above code is: "count, once, one, this, thus." 上面代码的输出是:“因此,一次,一次,一次,这个。”

I want to show the number of each words in the list as below. 我想在列表中显示每个单词的数量,如下所示。 [count][1], [once][2], [one][2], [this][1], [thus][2]. [count] [1],[once] [2],[one] [2],[this] [1],[thus] [2]。

In order to get above result; 为了获得以上结果; I try below code. 我尝试下面的代码。

    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

Above code only gives the number of words. 上面的代码仅给出字数。 Can someone advise me how can I print the word name and number of the words together such as in [once][2] format. 有人可以建议我如何一起打印单词名称和单词编号,例如[once] [2]格式。

Use a Counter dict to get the word count then just iterate over the .items : 使用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))

If you want that particular output you can wrap the elements in the str.format: 如果您想要特定的输出,则可以将元素包装在str.format中:

for k,v in cn.items():
    print("[{}][{}]".format(k,v))

[thus][2]
[count][1]
[one][2]
[once][2]
[this][1]

To get the output from highest count to lowest use .most_common: 要使输出从最高计数到最低计数,请使用.most_common:

cn = Counter(new_list)
for k,v in cn.most_common():
    print("[{}][{}]".format(k,v))

Output: 输出:

[once][2]
[thus][2]
[one][2]
[count][1]
[this][1]

If you want the data alphabetically from lowest to highest and from highest to lowest for the count you need to pass a key -x[1] to sorted to negate the count sorting the count from highest to lowest: 如果您希望数据按字母顺序从最低到最高,从最高到最低按字母顺序排列,则需要传递键-x[1]进行排序,以使计数取反,从而将计数从最高到最低排序:

for k, v in sorted(cn.items(), key=lambda x: (-x[1],x[0])):
    print("[{}][{}]".format(k, v))

Output: 输出:

[once][2]
[one][2]
[thus][2]
[count][1]
[this][1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM