学习笔记
#python 3.5.2
解压序列赋值给多个变量
>>> x=(1,2,3)>>> a,b,c=x>>> a1>>> b2>>> c3>>> data=['1','2','3','4','5']>>> _,a,_,_,b=data>>> a'2'>>> b'5'>>> y=(3,4)>>> a,b,c=yTraceback (most recent call last): File "", line 1, in a,b,c=yValueError: not enough values to unpack (expected 3, got 2)>>> >>> data=['1',2,(3,4),5]>>> a,b,L,c=data>>> L(3, 4)>>> a'1'>>> c5>>>
解压可迭代对象赋值给多个变量
>>> L=[1,2,3,4,5,6]>>> head,*tail=L>>> head1>>> tail[2, 3, 4, 5, 6]>>> head,*middle,tail=L>>> head1>>> middle[2, 3, 4, 5]>>> tail6>>> L=['aaa',10,(1,2,3),'dddd']>>> head,*middle,tail=L>>> head'aaa'>>> middle[10, (1, 2, 3)]>>> tail'dddd'>>> name,age,(a,b,c),addr=L>>> a1>>> b2>>> c3>>> addr'dddd'>>> age10>>>
保留最后N个元素
>>> from collections import deque>>> q=deque(maxlen=4)>>> q.append(1)>>> q.append(2)>>> q.append(3)>>> qdeque([1, 2, 3], maxlen=4)>>> q.append(4)>>> qdeque([1, 2, 3, 4], maxlen=4)>>> q.append(5)>>> qdeque([2, 3, 4, 5], maxlen=4)>>> q.append(6)>>> qdeque([3, 4, 5, 6], maxlen=4)>>> q.appendleft(0)>>> qdeque([0, 3, 4, 5], maxlen=4)>>> q.appendleft(-1)>>> qdeque([-1, 0, 3, 4], maxlen=4)>>>
查找最大或最小的N个元素
>>> import heapq>>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]>>> heapq.nlargest(3,nums)[42, 37, 23]>>> heapq.nlargest(4,nums)[42, 37, 23, 23]>>> heapq.nsmallest(2,nums)[-4, 1]>>> heapq.nsmallest(4,nums)[-4, 1, 2, 2]>>>
实现一个优先级队列
>>> import heapq>>> queue=[]>>> heapq.heappush(queue,(3,'three'))>>> heapq.heappush(queue,(2,'two'))>>> heapq.heappush(queue,(5,'five'))>>> heapq.heappush(queue,(1,'one'))>>> heapq.heappush(queue,(4,'four'))>>> heapq.heappop(queue)(1, 'one')>>> heapq.heappop(queue)(2, 'two')>>> heapq.heappop(queue)(3, 'three')>>> heapq.heappop(queue)(4, 'four')>>> heapq.heappop(queue)(5, 'five')>>>
字典中键映射多个值
>>> from collections import defaultdict>>> d=defaultdict(list)>>> d['a'].append(1)>>> d['a'].append(2)>>> d['a'].append(3)>>> d['b'].append(-1)>>> d['b'].append(-2)>>> d['c'].append('C')>>> ddefaultdict(, {'b': [-1, -2], 'c': ['C'], 'a': [1, 2, 3]})>>>
字典保持顺序
>>> from collections import OrderedDict>>> d=OrderedDict()>>> d['B']=2>>> d['A']=1>>> d['C']=3>>> d['F']=6>>> d['E']=5>>> dOrderedDict([('B', 2), ('A', 1), ('C', 3), ('F', 6), ('E', 5)])>>> for i in d: print(i,d[i])B 2A 1C 3F 6E 5>>> D=dict()>>> D['B']=2>>> D['A']=1>>> D['C']=3>>> D['F']=6>>> D['E']=5>>> for i in D: print(i,D[i])A 1F 6C 3B 2E 5>>>
字典的运算
>>> prices = { 'ACME': 45.23, 'AAPL': 612.78, 'IBM': 205.55, 'HPQ': 37.20, 'FB': 10.75}>>> min_price=min(zip(prices.values(),prices.keys()))>>> min_price(10.75, 'FB')>>> max_price=max(zip(prices.values(),prices.keys()))>>> max_price(612.78, 'AAPL')>>> prices_sorted=sorted(zip(prices.values(),prices.keys()))>>> prices_sorted[(10.75, 'FB'), (37.2, 'HPQ'), (45.23, 'ACME'), (205.55, 'IBM'), (612.78, 'AAPL')]>>> min(prices)'AAPL'>>> max(prices)'IBM'>>> min(prices,key=lambda x:prices[x])'FB'>>> max(prices,key=lambda x:prices[x])'AAPL'>>> prices[min(prices,key=lambda x:prices[x])]10.75>>> prices[max(prices,key=lambda x:prices[x])]612.78>>>
查找两个字典的相同点
>>> a = { 'x' : 1, 'y' : 2, 'z' : 3}>>> b = { 'w' : 10, 'x' : 11, 'y' : 2}>>> a.keys() & b.keys(){'x', 'y'}>>> a.keys() - b.keys(){'z'}>>> a.items() & b.items(){('y', 2)}>>>
删除序列相同元素并保持顺序
>>> def dedupe(items):seen =set()for item in items:if item not in seen:yield itemseen.add(item)>>> a = [1, 5, 2, 1, 9, 1, 5, 10]>>> list(dedupe(a))[1, 5, 2, 9, 10]
>>>
命名切片
>>> record = '....................100 .......513.25 ..........'>>> shares=slice(20,23)>>> price=slice(31,37)>>> cost=int(record[shares])*float(record[price])>>> cost51325.0>>>