激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - 5種Python統(tǒng)計次數(shù)方法技巧

5種Python統(tǒng)計次數(shù)方法技巧

2022-02-25 00:09小小程序員ol Python

這篇文章主要給大家分享的是5種Python統(tǒng)計次數(shù)方法技巧,文章主要包括字典 dict 統(tǒng)計、collections.defaultdict 統(tǒng)計、List count方法、集合(set)和列表(list)統(tǒng)計、collections.Counter方法,感興趣的小伙伴一起進入下面文章內(nèi)容吧

一、使用字典 dict 統(tǒng)計

循環(huán)遍歷出一個可迭代對象的元素,如果字典中沒有該元素,那么就讓該元素作為字典的鍵,并將該鍵賦值為1,如果存在則將該元素對應(yīng)的值加1。

?
1
2
3
4
5
6
7
8
9
lists = ['a','a','b',1,2,3,1]
count_dist = dict()
for i in lists:
    if i in count_dist:
        count_dist[i] += 1
    else:
        count_dist[i] = 1
print(count_dist)
# {'a': 2, 'b': 1, 1: 2, 2: 1, 3: 1}

二、使用 collections.defaultdict 統(tǒng)計

defaultdict(parameter) 接受一個類型參數(shù),例如:int、float、str 等。

傳遞進來的類型參數(shù),不是用來約束值的類型,更不是約束鍵的類型,而是當(dāng)鍵不存在時,實現(xiàn)一種值的初始化。

?
1
2
3
4
5
6
7
8
9
10
defaultdict(int) -- 初始化為0
defaultdict(float) -- 初始化為0.0
defaultdict(str) -- 初始化為''
from collections import defaultdict
lists = ['a','a','b',1,2,3,1]
count_dict = defaultdict(int)
for i in lists:
    count_dict[i] += 1
print(count_dict)
# defaultdict(<class 'int'>, {'a': 2, 'b': 1, 1: 2, 2: 1, 3: 1})

三、List count方法

count() 方法用于統(tǒng)計某個元素在列表中出現(xiàn)的次數(shù)。

使用語法:

?
1
2
# 使用語法
list.count(obj) # 返回次數(shù)

統(tǒng)計單個對象次數(shù):

?
1
2
3
4
5
6
# 統(tǒng)計單個對象次數(shù)
aList = [123, 'abc', 'good', 'abc', 123]
print("Count for 123 :", aList.count(123))
print("Count for abc :", aList.count('abc'))
# Count for 123 : 2
# Count for abc : 2

統(tǒng)計List中每一個對象次數(shù):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
test = ["aaa","bbb","aaa","aaa","ccc","ccc","ddd","aaa","ddd","eee","ddd"]
print(test.count("aaa"))
# 4
print(test.count("bbb"))
# 1
 
test_result = []
for i in test:
    if i not in test_result:
        test_result.append(i)
print(test_result)
 
for i in test_result:
    print(f"{i}:{test.count(i)}")
 
'''
4
1
['aaa', 'bbb', 'ccc', 'ddd', 'eee']
aaa:4
bbb:1
ccc:2
ddd:3
eee:1
'''

四、使用集合(set)和列表(list)統(tǒng)計

先用 set 去重,然后循環(huán)把每一個元素和對應(yīng)的次數(shù) list.count(item) 組成元組。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
'''
學(xué)習(xí)中遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流群:531509025
尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書!
'''
lists = ['a','a','b',1,2,3,1]
count_set = set(lists)
print(count_set) # 集合去重
# {1, 2, 3, 'b', 'a'}
 
count_list = list()
for i in count_set:
    count_list.append((i, lists.count(i)))
print(count_list)   
# [(1, 2), (2, 1), (3, 1), ('b', 1), ('a', 2)]

五、collections.Counter方法

Counter 是一個容器對象,使用 collections 模塊中的 Counter 類可以實現(xiàn) hash 對象的統(tǒng)計。

Counter 是一個無序的容器類型,以字典的鍵值對形式存儲,其中元素作為 key,其計數(shù)作為 value。

計數(shù)值可以是任意的 Interger(包括0和負數(shù))。

Counter() 對象還有幾個可調(diào)用的方法:

  • most_common(n) -- TOP n 個出現(xiàn)頻率最高的元素
  • elements -- 獲取所有的鍵 通過list轉(zhuǎn)化
  • update -- 增加對象
  • subtrct -- 刪除對象
  • 下標(biāo)訪問 a['xx'] --不存在時返回0
?
1
2
import collections
c = collections.Counter('helloworld')

直接顯示各個元素頻次

?
1
2
print(c)
# Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, 'w': 1, 'r': 1, 'd': 1})

使用 most_common顯示最多的n個元素

當(dāng)多個元素計數(shù)值相同時,排列是無確定順序的。

?
1
2
print(c.most_common(3))
# [('l', 3), ('o', 2), ('h', 1)]

使用數(shù)組下標(biāo)獲取,類似字典方式:

?
1
2
print("The number of 'o':", c['o'])
# The number of 'o': 2

統(tǒng)計列表: (只要列表中對象都是可以哈希的)

?
1
2
3
4
5
6
7
8
9
10
import collections
x = [1,2,3,4,5,6,7,8,1,8,8,8,4,3,5]
c = collections.Counter(x)
print(c)
# Counter({1: 2, 2: 1, 3: 2, 4: 2, 5: 2, 6: 1, 7: 1, 8: 4})
print(c.most_common(3))
# [(8, 4), (1, 2), (3, 2)]
dictc = dict(c) # 轉(zhuǎn)換為字典
print(dictc)
# {1: 2, 2: 1, 3: 2, 4: 2, 5: 2, 6: 1, 7: 1, 8: 4}

如果列表中有 unhashalbe 對象,例如:可變的列表,是無法統(tǒng)計的。

元組也可以統(tǒng)計。

?
1
2
c = collections.Counter([[1,2], "hello", 123, 0.52])
# TypeError: unhashable type: 'list'

得到 Counter 計數(shù)器對象之后,還可以在此基礎(chǔ)上進行增量更新。

elements() -- 返回迭代器

元素排列無確定順序,個數(shù)小于1的元素不被包含。

?
1
2
3
4
5
6
7
8
9
10
11
'''
學(xué)習(xí)中遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流群:531509025
尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書!
'''
import collections
c = collections.Counter(a=4,b=2,c=1)
print(c)
# Counter({'a': 4, 'b': 2, 'c': 1})
 
list(c.elements())
# ['a', 'a', 'a', 'a', 'b', 'b', 'c']

subtract函數(shù) -- 減去元素

?
1
2
3
4
5
6
7
8
9
10
11
12
13
import collections
c = collections.Counter(["a","b","c","a"])
print(c)
# Counter({'a': 2, 'b': 1, 'c': 1})
print(list(c.elements())) # 展開
# ['a', 'a', 'b', 'c']
 
# 減少元素
c.subtract(["a","b"])
print(c)
# Counter({'a': 1, 'c': 1, 'b': 0})
print(list(c.elements()))
# ['a', 'c']

update函數(shù) -- 增加元素

在進行增量計數(shù)時候,update函數(shù)非常有用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
'''
學(xué)習(xí)中遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流群:531509025
尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書!
'''
import collections
c = collections.Counter(["a","b","c","a"])
print(c)
# Counter({'a': 2, 'b': 1, 'c': 1})
print(list(c.elements())) # 展開
# ['a', 'a', 'b', 'c']
 
c.update(["a","d"])
print(c)
# Counter({'a': 3, 'b': 1, 'c': 1, 'd': 1})
print(list(c.elements()))
# ['a', 'a', 'a', 'b', 'c', 'd']

del函數(shù) -- 刪除鍵

當(dāng)計數(shù)值為0時,并不意味著元素被刪除,刪除元素應(yīng)當(dāng)使用del

?
1
2
3
4
5
6
7
8
9
10
11
12
import collections
c = collections.Counter('helloworld')
print(c)
# Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, 'w': 1, 'r': 1, 'd': 1})
 
c["d"] = 0
print(c)
# Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, 'w': 1, 'r': 1, 'd': 0})
 
del c["l"]
print(c)
# Counter({'o': 2, 'h': 1, 'e': 1, 'w': 1, 'r': 1, 'd': 0})

到此這篇關(guān)于5種Python統(tǒng)計次數(shù)方法技巧的文章就介紹到這了,更多相關(guān)Python內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://www.cnblogs.com/python960410445/p/15435330.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 夜夜夜精品视频 | 免费一级肉体全黄毛片 | 操操影视| 国产一区二区在线免费播放 | 99视频观看 | 免费观看又色又爽又黄的崩锅 | 日韩在线视频在线观看 | 国产精选电影免费在线观看网站 | 91精品欧美一区二区三区 | 在线天堂中文在线资源网 | 97超级碰碰人国产在线观看 | 激情久久婷婷 | 日韩精品一区二区免费视频 | 免费一级欧美大片视频 | 国产精品一区二区视频 | 免费黄色入口 | 最新黄色电影网站 | 日本一区二区不卡高清 | 欧美成人黄色 | 国产精品成人免费一区久久羞羞 | 精品一区二区三区免费毛片爱 | 国产精彩视频在线 | 成人免费在线网 | 国产精品夜色视频一级区 | 日本欧美一区二区 | 五月婷六月丁香狠狠躁狠狠爱 | 2017亚洲男人天堂 | 99这里有精品 | 成人性生活视频在线播放 | 国产精选电影免费在线观看 | 永久免费毛片 | 国产精品久久久av | 最新av免费网址 | 久久久久久久久久91 | 国产伦精品一区二区三区 | 国产精品免费久久久 | 爱操影视 | 免费在线成人网 | 极品五月天 | 国产在线精品区 | 国产青草视频在线观看视频 |