繁体   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)

输出:

类型错误:只能将 str(不是“int”)连接到 str

尝试这个。 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'))

输出

2

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

您可以编写自己的计数函数。

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

print(find_genre('pop'))
#2

对 200 万个列表的timeit分析。 撰写本文时的结果(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)

现在,每个人都建议包括我在内的自定义计数功能。

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)

只是为了比较我不建议使用),我编写了一些其他函数来计算计数。

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)

您的代码中出现错误,因为您使用count来计算类型的计数,并再次使用count作为循环变量。 在每次迭代中, count变成str并且我们不能将str添加到int类型。

我会使用 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'])

输出
1

另一种可能的解决方案,如果您特别想修复当前的方法:

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)

输出
2

您的主要问题是,以与命名计数器变量相同的方式命名 for 循环变量会让您感到困惑(并且行为也不同)。

正如 Johnny Mop 在评论中指出的那样:

这一行在流派列表中计数使计数成为一个字符串。

正如约翰尼·莫普在评论中已经指出的那样, 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)

或者以更pythonic的方式使用列表理解:

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

或者使用@Pitto 建议的集合(当您第一次开始编程时可能不那么直观)

首先,您在迭代中使用与列表中的索引相同的变量名称。

其次,您应该在参数pop使用撇号,例如'pop'

希望这可以帮助:

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