python中counter_Python collections模块中counter()的详细说明,Pythoncollections,之,Counter,详解...
collections模塊 ==> Python標準庫,數據結構常用的模塊;collections包含了一些特殊的容器,針對Python內置的容器,例如list、dict、set和tuple,提供了另一種選擇。
collections模塊常用類型有:
計數器(
Counter
)
dict的子類,計算可hash的對象
請點擊
Counter
雙向隊列(
deque
)
類似于list的容器,可以快速的在隊列頭部和尾部添加、刪除元素
請點擊
deque
默認字典(defaultdict)
dict的子類,可以調用提供默認值的函數
有序字典(OrderedDict)
dict的子類,可以記住元素的添加順序
可命名元組(namedtuple)
可以創建包含名稱的tuple
Counter()
主要功能:可以支持方便、快速的計數,將元素數量統計,然后計數并返回一個字典,鍵為元素,值為元素個數。
from collections import Counter
list1 = ["a", "a", "a", "b", "c", "c", "f", "g", "g", "g", "f"]
dic = Counter(list1)
print(dic)
#結果:次數是從高到低的
#Counter({'a': 3, 'g': 3, 'c': 2, 'f': 2, 'b': 1})
print(dict(dic))
#結果:按字母順序排序的
#{'a': 3, 'b': 1, 'c': 2, 'f': 2, 'g': 3}
print(dic.items()) #dic.items()獲取字典的key和value
#結果:按字母順序排序的
#dict_items([('a', 3), ('b', 1), ('c', 2), ('f', 2), ('g', 3)])
print(dic.keys())
#結果:
#dict_keys(['a', 'b', 'c', 'f', 'g'])
print(dic.values())
#結果:
#dict_values([3, 1, 2, 2, 3])
print(sorted(dic.items(), key=lambda s: (-s[1])))
#結果:按統計次數降序排序
#[('a', 3), ('g', 3), ('c', 2), ('f', 2), ('b', 1)]
for i, v in dic.items():
if v == 1:
print(i)
#結果:
#b
from collections import Counter
str1 = "aabbfkrigbgsejaae"
print(Counter(str1))
print(dict(Counter(str1)))
#結果:
#Counter({'a': 4, 'b': 3, 'g': 2, 'e': 2, 'f': 1, 'k': 1, 'r': 1, 'i': 1, 's': 1, 'j': 1})
#{'a': 4, 'b': 3, 'f': 1, 'k': 1, 'r': 1, 'i': 1, 'g': 2, 's': 1, 'e': 2, 'j': 1}
dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2}
print(Counter(dic1))
Counter對象支持以下三個字典不支持的方法,update()字典支持
most_common()
返回一個
列表
,包含counter中n個最大數目的元素,如果忽略n或者為None,most_common()將會返回counter中的所有元素,元素有著相同數目的將會選擇出現早的元素
list1 = ["a", "a", "a", "b", "c", "f", "g", "g", "c", "11", "g", "f", "10", "2"]
print(Counter(list1).most_common(3))
#結果:[('a', 3), ('g', 3), ('c', 2)]
#"c"、"f"調換位置,結果變化
list2 = ["a", "a", "a", "b", "f", "c", "g", "g", "c", "11", "g", "f", "10", "2"]
print(Counter(list2).most_common(3))
#結果:[('a', 3), ('g', 3), ('f', 2)]
elements()
返回一個
迭代器
,每個元素重復的次數為它的數目,順序是任意的順序,如果一個元素的數目少于1,那么elements()就會忽略它
list2 = ["a", "a", "abdz", "abc", "f", "c", "g", "g", "c", "c1a1", "g", "f", "111000", "b10"]
print(''.join(Counter(list2).elements()))
#結果:aaabdzabcffccgggc1a1111000b10
print(''.join(list2))
#結果:aaabdzabcfcggcc1a1gf111000b10
dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2, "e": 0}
print(Counter(dic1))
print(list(Counter(dic1).elements()))
#結果:
#Counter({'b': 4, 'a': 3, 'c': 0, 'e': 0, 'd': -2})
#['a', 'a', 'a', 'b', 'b', 'b', 'b']
update()
從一個可迭代對象(可迭代對象是一個元素序列,而非(key,value)對構成的序列)中或者另一個映射(或counter)中所有元素相加,是數目相加而非替換它們
dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2, "e": 0}
dic2 = {'a': 3, 'b': 4, 'c': 0, 'd': 2, "e": -1, "f": 6}
a = Counter(dic1)
print(a)
#結果:Counter({'b': 4, 'a': 3, 'c': 0, 'e': 0, 'd': -2})
b = Counter(dic2)
print(b)
#結果:Counter({'f': 6, 'b': 4, 'a': 3, 'd': 2, 'c': 0, 'e': -1})
a.update(b)
print(a)
#結果:Counter({'b': 8, 'a': 6, 'f': 6, 'c': 0, 'd': 0, 'e': -1})
subtract()
從一個可迭代對象中或者另一個映射(或counter)中,元素相減,是數目相減而不是替換它們
dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2, "e": 0}
dic2 = {'a': 3, 'b': 4, 'c': 0, 'd': 2, "e": -1, "f": 6}
a = Counter(dic1)
print(a)
#結果:Counter({'b': 4, 'a': 3, 'c': 0, 'e': 0, 'd': -2})
b = Counter(dic2)
print(b)
#結果:Counter({'f': 6, 'b': 4, 'a': 3, 'd': 2, 'c': 0, 'e': -1})
a.subtract(b)
print(a)
#結果:Counter({'e': 1, 'a': 0, 'b': 0, 'c': 0, 'd': -4, 'f': -6})
總結
以上是生活随笔為你收集整理的python中counter_Python collections模块中counter()的详细说明,Pythoncollections,之,Counter,详解...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python第三方工具箱_我的Pytho
- 下一篇: 服务器上出现应用程序错误。此应用程序的当