一、使用字典 dict 統計
循環遍歷出一個可迭代對象的元素,如果字典中沒有該元素,那么就讓該元素作為字典的鍵,并將該鍵賦值為1,如果存在則將該元素對應的值加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 統計
defaultdict(parameter)
接受一個類型參數,例如:int、float、str 等。
傳遞進來的類型參數,不是用來約束值的類型,更不是約束鍵的類型,而是當鍵不存在時,實現一種值的初始化。
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()
方法用于統計某個元素在列表中出現的次數。
使用語法:
1
2
|
# 使用語法 list .count(obj) # 返回次數 |
統計單個對象次數:
1
2
3
4
5
6
|
# 統計單個對象次數 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 |
統計List中每一個對象次數:
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)統計
先用 set
去重,然后循環把每一個元素和對應的次數 list.count(item)
組成元組。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和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
類可以實現 hash
對象的統計。
Counter
是一個無序的容器類型,以字典的鍵值對形式存儲,其中元素作為 key,其計數作為 value。
計數值可以是任意的 Interger
(包括0和負數)。
Counter() 對象還有幾個可調用的方法:
-
most_common(n)
-- TOP n 個出現頻率最高的元素 -
elements
-- 獲取所有的鍵 通過list轉化 -
update
-- 增加對象 -
subtrct
-- 刪除對象 - 下標訪問 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個元素
當多個元素計數值相同時,排列是無確定順序的。
1
2
|
print (c.most_common( 3 )) # [('l', 3), ('o', 2), ('h', 1)] |
使用數組下標獲取,類似字典方式:
1
2
|
print ( "The number of 'o':" , c[ 'o' ]) # The number of 'o': 2 |
統計列表: (只要列表中對象都是可以哈希的)
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) # 轉換為字典 print (dictc) # {1: 2, 2: 1, 3: 2, 4: 2, 5: 2, 6: 1, 7: 1, 8: 4} |
如果列表中有 unhashalbe
對象,例如:可變的列表,是無法統計的。
元組也可以統計。
1
2
|
c = collections.Counter([[ 1 , 2 ], "hello" , 123 , 0.52 ]) # TypeError: unhashable type: 'list' |
得到 Counter
計數器對象之后,還可以在此基礎上進行增量更新。
elements()
-- 返回迭代器
元素排列無確定順序,個數小于1的元素不被包含。
1
2
3
4
5
6
7
8
9
10
11
|
''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和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
函數 -- 減去元素
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
函數 -- 增加元素
在進行增量計數時候,update
函數非常有用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和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
函數 -- 刪除鍵
當計數值為0時,并不意味著元素被刪除,刪除元素應當使用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}) |
到此這篇關于5種Python統計次數方法技巧的文章就介紹到這了,更多相關Python內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/python960410445/p/15435330.html