简体   繁体   English

列表python中元素(字符串)的计数

[英]counting of elements (strings) in list python

genres_list = ['rock', 'pop', 'folk', 'dance', 'rusrap', 'ruspop', 'world', 'electronic', 'alternative', 'children', 'rnb', 'hip', 'jazz','postrock', 'latin', 'classical', 'metal', 'reggae', 'tatar','blues','pop', 'jazz']

# write function to count the number of specific genre in genres_list 

def find_genre(genre):
    count=0
    for count in genres_list:
        if count == genre:
            count=count+1
    return count

number=find_genre(pop)
print(number)

output:输出:

TypeError: can only concatenate str (not "int") to str类型错误:只能将 str(不是“int”)连接到 str

Try this.尝试这个。 list has a method to count the number occurences of an element. list有一个方法来计算元素出现的次数。

genres_list = ['rock', 'pop', 'folk', 'dance', 'rusrap', 'ruspop', 'world', 'electronic', 'alternative', 'children', 'rnb', 'hip', 'jazz','postrock', 'latin', 'classical', 'metal', 'reggae', 'tatar','blues','pop', 'jazz']

print(genres_list.count('pop'))

output输出

2

list.count() complexity is O(n) . list.count()复杂度是O(n)

You can write your own count function.您可以编写自己的计数函数。

def find_genre(genre):
    count=0
    for _genre in genres_list:
        if _genre==genre:
            count+=1
    return count

print(find_genre('pop'))
#2

timeit analysis over a list of size 2 million.对 200 万个列表的timeit分析。 Results as of this writing (python 3.7, windows 10)撰写本文时的结果(python 3.7,windows 10)

In [38]: timeit genres_list.count('pop') #My answer
26.6 ms ± 939 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [40]: timeit Counter(genres_list)['pop'] #Pitto's answer using collections.Counter
92.5 ms ± 751 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

Now, custom count function everyone suggested including me.现在,每个人都建议包括我在内的自定义计数功能。

def find_genre(genre):
    count=0
    for _genre in genres_list:
        if _genre==genre:
            count+=1
    return count

In [42]: timeit find_genre('pop')
63.9 ms ± 803 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

Just for the sake of comparison ( Which I don't recommend using ) I wrote some other functions to calculate count.只是为了比较我不建议使用),我编写了一些其他函数来计算计数。

In [36]: timeit sum(list(map(lambda x:x=='pop',genres_list)))
334 ms ± 13.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [37]: timeit len(list(filter(lambda x:x=='pop',genres_list)))
188 ms ± 18.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [44]: timeit ' '.join(genres_list).count('pop')
41.5 ms ± 2.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Error occurred in your code because you use count for calculating the count of a genre and again you used count as a looping variable.您的代码中出现错误,因为您使用count来计算类型的计数,并再次使用count作为循环变量。 In every iteration count becomes a str and we can't add str to a int type.在每次迭代中, count变成str并且我们不能将str添加到int类型。

I would use Counter to achieve this result:我会使用 Counter 来实现这个结果:

import collections

genres_list = ['rock', 'pop', 'folk', 'dance', 'rusrap', 'ruspop', 'world', 'electronic', 'alternative', 'children', 'rnb', 'hip', 'jazz','postrock', 'latin', 'classical', 'metal', 'reggae', 'tatar','blues','pop', 'jazz']

counted = collections.Counter(genres_list)
print(counted['rock'])

Output输出
1 1

Another possible solution, if you specifically want to fix your current approach:另一种可能的解决方案,如果您特别想修复当前的方法:

def find_genre(genre):
    count=0
    for current_genre in genres_list:
        if current_genre == genre:
            count=count+1
    return count

number=find_genre('pop')
print(number)

Output输出
2 2

Your main issue was that naming the for loop variable in the same way that you named the counter variable was confusing you (and was behaving differently too).您的主要问题是,以与命名计数器变量相同的方式命名 for 循环变量会让您感到困惑(并且行为也不同)。

As pointed out in comment from Johnny Mop:正如 Johnny Mop 在评论中指出的那样:

This line for count in genres_list: makes count a string.这一行在流派列表中计数使计数成为一个字符串。

As Johnny Mopp already pointed out in the comments, for count in genres_list overwrites your count variable to be a string, so you could either do:正如约翰尼·莫普在评论中已经指出的那样, for count in genres_list中的count将您的count变量覆盖为一个字符串,因此您可以这样做:

def find_genre(searched_genre):
    count=0
    for genre in genres_list:
        if searched_genre == genre:
            count = count+1
    return count

number = find_genre('pop')
print(number)

Or in a more pythonic way using list comprehension:或者以更pythonic的方式使用列表理解:

def find_genre(searched_genre):
    return len([genre for genre in genres_list if genre == searched_genre])

Or use a collection as @Pitto suggested (which might not be as intuitive when you first start programming)或者使用@Pitto 建议的集合(当您第一次开始编程时可能不那么直观)

First, you're using the same variable name in your iteration as the index in your list.首先,您在迭代中使用与列表中的索引相同的变量名称。

Second, you should use apostrophes in the argument pop like this 'pop' .其次,您应该在参数pop使用撇号,例如'pop'

Hope this helps:希望这可以帮助:

genres_list = ['rock', 'pop', 'folk', 'dance', 'rusrap', 'ruspop', 'world', 'electronic', 'alternative', 'children', 'rnb', 'hip', 'jazz','postrock', 'latin', 'classical', 'metal', 'reggae', 'tatar','blues','pop', 'jazz']

# write function to count the number of specific genre in genres_list
def find_genre(genre):
    num=0
    for count in genres_list:
        if count == genre:
            num=num+1
    return num

number=find_genre('pop')
print(number)

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

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