av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

創(chuàng)新互聯(lián)Python教程:python如何查詢列表中不同元素個數(shù)?

python中可以使用collections.Counter(list)方法查詢列表中不同元素個數(shù)。

Counter中文意思是計數(shù)器,也就是我們常用于統(tǒng)計的一種數(shù)據(jù)類型,在使用Counter之后可以讓我們的代碼更加簡單易讀。

示例:

#統(tǒng)計詞頻
colors = ['red', 'blue', 'red', 'green', 'blue', 'blue']
result = {}
for color in colors:
    if result.get(color)==None:
        result[color]=1
    else:
        result[color]+=1
print (result)
#{'red': 2, 'blue': 3, 'green': 1}

用Counter實現(xiàn):

from collections import Counter
colors = ['red', 'blue', 'red', 'green', 'blue', 'blue']
c = Counter(colors)
print (dict(c))

輸出結(jié)果

{'red': 2, 'blue': 3, 'green': 1}


文章名稱:創(chuàng)新互聯(lián)Python教程:python如何查詢列表中不同元素個數(shù)?
轉(zhuǎn)載來于:http://uogjgqi.cn/article/djsopis.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流