1.概览
key | value |
---|
ChainMap | 类似于字典的类,将多个字典组合成一个字典,并且支持字典的所有操作 |
Counter | 用于计数 |
2.ChainMap
from collections import ChainMap
a = {"a": 1}
b = {"b": 2}
c = {"c": 3}
new_dict = ChainMap(a, b, c)
print(new_dict)
new_dict["e"] = 111
print(new_dict)
new_dict.update({"f": 5})
print(new_dict)
del new_dict["a"]
print(new_dict)
new_dict["a"] = "改变a的value"
print(new_dict)
new_dict.items()
new_dict.keys()
new_dict.values()
new_dict.get("A", 222)
print(new_dict.new_child())
print(new_dict.new_child(m=b))
print(new_dict.maps)
3.Counter
from collections import Counter
new_dict = Counter("helloword")
print(new_dict)
new_dict = "hello word".split()
new_dict = Counter(new_dict)
print(new_dict)
new_dict = Counter({"a": 1, "b": 2})
print(new_dict)
new_dict = Counter(cats=3, dogs=4, haha=5, heheh=6)
print(new_dict)
print(list(new_dict.elements()))
print(new_dict.most_common(2))
aa = Counter(a=3, b=2, c=2, e=5)
bb = Counter(a=1, b=2, c=3, d=4)
aa.subtract(bb)
print(aa)