簡體   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