python常用模塊-collections之Counter模塊


# Counter模塊:計算元素個數
# 注意:不能處理多維的數據結構
>>> s1 = "uwerhbbsmndfwehajksdhfjsss" # 處理字符串
>>> dict(Counter(s1)) # 返回每個元素出現的個數,以字典的形式返回
{'u': 1, 'w': 2, 'e': 2, 'r': 1, 'h': 3, 'b': 2, 's': 5, 'm': 1, 'n': 1, 'd': 2, 'f': 2, 'a': 1, 'j': 2, 'k': 1}
>>> list(Counter(s1))  # 返回key組成的列表
['u', 'w', 'e', 'r', 'h', 'b', 's', 'm', 'n', 'd', 'f', 'a', 'j', 'k']

>>> l1 = [1,2,3,'hello']  # 處理列表
>>> dict(Counter(l1))
{1: 1, 2: 1, 3: 1, 'hello': 1}
>>> list(Counter(l1))
[1, 2, 3, 'hello']
>>> l2 = [12,34,"wer",[11,22,3],"python"]  # 注意:無法處理多維列表的
>>> dict(Counter(l2))
TypeError: unhashable type: 'list'

>>> t1 = ("hello", "world", 'hello', 'python') # 處理元組,也是無法處理多維的元組
>>>
>>> dict(Counter(t1))
{'hello': 2, 'world': 1, 'python': 1}
>>> list(Counter(t1))
['hello', 'world', 'python']

>>> d1 = {'ab':3, 'c':5, (1,2):"d"}  # 處理字典
>>> dict(Counter(d1))
{'ab': 3, 'c': 5, (1, 2): 'd'}
>>> list(Counter(d1))
['ab', 'c', (1, 2)]

# most_common(num) 按照元素出現的次數進行從高到低的排序,返回前num個元素的字典
>>> Counter(s1).most_common() # 字符串
[('s', 5), ('h', 3), ('w', 2), ('e', 2), ('b', 2), ('d', 2), ('f', 2), ('j', 2), ('u', 1), ('r', 1), ('m', 1), ('n', 1), ('a', 1), ('k', 1)]
>>> Counter(s1).most_common(2)
[('s', 5), ('h', 3)]

>>> Counter(l1).most_common() # 處理列表
[(1, 1), (2, 1), (3, 1), ('hello', 1)]
>>> Counter(l1).most_common(2)
[(1, 1), (2, 1)]

>>> Counter(t1).most_common() # 處理元組
[('hello', 2), ('world', 1), ('python', 1)]
>>> Counter(t1).most_common(1)
[('hello', 2)]

>>> d1
{'ab': 3, 'c': 5, (1, 2): 'd'} # key為一個序列時不可以使用這個方法的
>>> Counter(d1).most_common()
TypeError: '<' not supported between instances of 'int' and 'str'
>>> d2 = {'ab':3, 'c':5, 12:23}
[(12, 23), ('c', 5), ('ab', 3)]
>>> Counter(d2).most_common(1)
[(12, 23)]

# elements返回經過計算器Counter后的元素,返回的是一個迭代器
>>> Counter(s1).elements() # 處理字符串
<itertools.chain object at 0x0000026519B81048>
>>> "".join(Counter(s1).elements())
'uwweerhhhbbsssssmnddffajjk'

>>> Counter(l1).elements() # 處理列表
<itertools.chain object at 0x0000026519B81048>
>>> for i in Counter(l1).elements():
...     print(i)
...
1
2
3
hello

>>> Counter(t1).elements() # 處理元組
<itertools.chain object at 0x0000026519B73D88>
>>> iter1 = Counter(t1).elements()
>>> iter1.__next__()
'hello'
>>> iter1.__next__()
'hello'
>>> iter1.__next__()
'world'
>>> iter1.__next__()
'python'
>>> iter1.__next__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

>>> d2  # 處理字典
{'ab': 3, 'c': 5, 12: 23}
>>> Counter(d2).elements()
<itertools.chain object at 0x0000026519B81048>
>>> list(Counter(d2).elements())
['ab', 'ab', 'ab', 'c', 'c', 'c', 'c', 'c', 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12]

#update更新,做加法,加上對應的個數
>>> s2 = Counter(s1) # 處理字符串
>>> s2.update('XXXXSSS')  # 這個方法並沒改變原來的字符串,而是重新創建了一個字符串
>>> id(s2)
2633245598056
>>> id(s1)
2633246378304
>>> dict(s2)
{'u': 1, 'w': 2, 'e': 2, 'r': 1, 'h': 3, 'b': 2, 's': 5, 'm': 1, 'n': 1, 'd': 2, 'f': 2, 'a': 1, 'j': 2, 'k': 1, 'X': 4, 'S': 3}

>>> l1   # 處理列表,不是改變原來的列表,而是重新創建了一個列表
[1, 2, 3, 'hello']
>>> l2 = Counter(l1)
>>> id(l2)
2633246372872
>>> id(l1)
2633246352008
>>> l2.update([99,77,"world"])
>>> l2
Counter({1: 1, 2: 1, 3: 1, 'hello': 1, 99: 1, 77: 1, 'world': 1})

>>> t2 = Counter(t1) #處理元組,不是改變原來的元組,而是重新創建一個元組
>>> id(t1)
2633245381080
>>> id(t2)
2633246373064
>>> t1
('hello', 'world', 'hello', 'python')
>>> t2.update('11', '11', '22', '22') #只接受一個參數
TypeError: expected at most 1 arguments, got 4
>>> t2.update(('11', '11', '22', '22'))
>>> dict(t2)
{'hello': 2, 'world': 1, 'python': 1, '11': 2, '22': 2}
>>> list(t2) 
['hello', 'world', 'python', '11', '22']


>>> d2
{'ab': 3, 'c': 5, 12: 23}
>>> d3 = Counter(d2)
>>> id(d2)
263324639992
>>> id(d3)
2633246373256
>>> dict(d3)
{'ab': 3, 'c': 5, 12: 56}
>>> list(d3)
['ab', 'c', 12]

# 可以使用字典里面的方法:key,values,items,get。。。
>>> Counter(s1).items()
dict_items([('u', 1), ('w', 2), ('e', 2), ('r', 1), ('h', 3), ('b', 2), ('s', 5), ('m', 1), ('n', 1), ('d', 2), ('f', 2), ('a', 1), ('j', 2), ('k', 1)])
>>> list(Counter(l1).keys())
[1, 2, 3, 'hello']
>>> tuple(Counter(t1).values())
(2, 1, 1)
>>> dict(Counter(d2)).get(12)
23

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM