日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何查詢列表中不同元素個(gè)數(shù)?

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

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

示例:

#統(tǒng)計(jì)詞頻
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實(shí)現(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}


本文標(biāo)題:創(chuàng)新互聯(lián)Python教程:python如何查詢列表中不同元素個(gè)數(shù)?
網(wǎng)頁路徑:http://www.5511xx.com/article/djsopis.html