日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

collections系列

發(fā)布時(shí)間:2023/12/18 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 collections系列 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
class Counter(dict):
  Counter類繼承dict類、繼承了dict的所有功能

計(jì)數(shù)器:
例:
import
collections obj = collections.Counter('sdkasdioasdjoasjdoasd') print(obj)

得:
Counter({'s': 5, 'd': 5, 'a': 4, 'o': 3, 'j': 2, 'k': 1, 'i': 1})

?

拿到前幾位:
""" 數(shù)量大于等n的所有元素和計(jì)數(shù)器 """ def most_common(self, n=None):'''List the n most common elements and their counts from the mostcommon to the least. If n is None, then list all element counts.>>> Counter('abcdeabcdabcaba').most_common(3)[('a', 5), ('b', 4), ('c', 3)]'''# Emulate Bag.sortedByCount from Smalltalkif n is None:return sorted(self.items(), key=_itemgetter(1), reverse=True)return _heapq.nlargest(n, self.items(), key=_itemgetter(1)) 例:
import
collections obj = collections.Counter('sdkasdioasdjoasjdoasd')
#elements  =>> 原生的值
#obj    =>> 處理完的數(shù)據(jù)
print(obj) ret = obj.most_common(4) print(ret)

得:

Counter({'d': 5, 's': 5, 'a': 4, 'o': 3, 'j': 2, 'k': 1, 'i': 1})
[('d', 5), ('s', 5), ('a', 4), ('o', 3)]

?

打印所有的元素、沒有個(gè)數(shù)
for
item in obj.elements():print(item)

?

dict
otems()  keys()  values()

couter(dic)#繼承了所有字典的方法
otems()  keys()  values()
“r”:3
elements()#所有的元素


for
k,v in obj.items():print(k,v)

?

? import collections #obj = collections.Counter('sjhdaosdijoasjdoasd') obj = collections.Counter(['11','22','33','22']) print(obj)ret = obj.most_common(4) print(ret)

得:

Counter({'22': 2, '11': 1, '33': 1})
[('22', 2), ('11', 1), ('33', 1)]

?

def update(*args, **kwds):
更新計(jì)數(shù)器,其實(shí)就是增加;如果原來沒有,則新建,如果有則加一
'''Like dict.update() but add counts instead of replacing them.Source can be an iterable, a dictionary, or another Counter instance.>>> c = Counter('which')>>> c.update('witch') # add elements from another iterable>>> d = Counter('watch')>>> c.update(d) # add elements from another counter>>> c['h'] # four 'h' in which, witch, and watch4'''# The regular dict.update() operation makes no sense here because the# replace behavior results in the some of original untouched counts# being mixed-in with all of the other counts for a mismash that# doesn't have a straight-forward interpretation in most counting# contexts. Instead, we implement straight-addition. Both the inputs# and outputs are allowed to contain zero and negative counts.if not args:raise TypeError("descriptor 'update' of 'Counter' object ""needs an argument")self, *args = argsif len(args) > 1:raise TypeError('expected at most 1 arguments, got %d' % len(args))iterable = args[0] if args else Noneif iterable is not None:if isinstance(iterable, Mapping):if self:self_get = self.getfor elem, count in iterable.items():self[elem] = count + self_get(elem, 0)else:super(Counter, self).update(iterable) # fast path when counter is emptyelse:_count_elements(self, iterable)if kwds:self.update(kwds) 例(增加):
import
collections obj = collections.Counter(['11','22','33','22']) print(obj)obj.update(['eric','11','11'])#增加 print(obj)

得:

Counter({'22': 2, '11': 1, '33': 1})
Counter({'11': 3, '22': 2, '33': 1, 'eric': 1})

?

例(減去):
import
collections obj = collections.Counter(['11','22','33','22']) print(obj) obj.subtract(['eric','11','11'])#刪除 print(obj)
得:

Counter({'22': 2, '11': 1, '33': 1})
Counter({'22': 2, '33': 1, '11': -1, 'eric': -1})

?



轉(zhuǎn)載于:https://www.cnblogs.com/mrzuo/p/7094911.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的collections系列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。