from collections import namedtuple
Point = namedtuple('Point',['x','y'])
p = Point(1,2)
p
Point(x=1, y=2)
from collections import deque
q = deque(['a','b','c'])
q.append('x')
q.appendleft('y')
q
deque(['y', 'a', 'b', 'c', 'x'])
from collections import OrderedDict
d = dict([('a',1),('b',2),('c',3)])
d
{'a': 1, 'b': 2, 'c': 3}
od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
od
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
from collections import OrderedDict
class LastUpdatedOrderedDict(OrderedDict):
def __init__(self,capacity):
super(LastUpdatedOrderedDict,self).__init__()
self._capacity = capacity
def __setitem__(self,key,value):
containsKey = 1 if key in self else 0
if len(self) - containsKey >= self._capacity:
last = self.popitem(last=False)
print("remove",last)
if containsKey:
del self[key]
print('set',(key,value))
else:
print('add',(key,value))
OrderedDict.__setitem__(self,key,value)
from collections import Counter
c = Counter()
for ch in "programming":
c[ch] = c[ch]+1
c
Counter({'a': 1, 'g': 2, 'i': 1, 'm': 2, 'n': 1, 'o': 1, 'p': 1, 'r': 2})
import hashlib
md5 = hashlib.md5()
md5.update('how to use md5 in python hashlib?'.encode('utf-8'))
print(md5.hexdigest())
d26a53750bc40b38b65a520292f69306
import itertools
for c in itertools.chain('ABC','XYZ'):
print(c)
A
B
C
X
Y
Z
for key,group in itertools.groupby("AAABBBCCAAA"):
print(key,list(group))
A ['A', 'A', 'A']
B ['B', 'B', 'B']
C ['C', 'C']
A ['A', 'A', 'A']