学习笔记
#python 3.5.2
解压序列赋值给多个变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
>>> x=(1,2,3) >>> a,b,c=x >>> a 1 >>> b 2 >>> c 3 >>> data=[ '1' , '2' , '3' , '4' , '5' ]
>>> _,a,_,_,b=data >>> a '2' >>> b '5' >>> y=(3,4) >>> a,b,c=y Traceback (most recent call last): File "<pyshell#14>" , line 1, in <module>
a,b,c=y
ValueError: 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' >>> c 5 >>> |
解压可迭代对象赋值给多个变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
>>> L=[1,2,3,4,5,6] >>> head ,* tail =L
>>> head
1 >>> tail
[2, 3, 4, 5, 6] >>> head ,*middle, tail =L
>>> head
1 >>> middle [2, 3, 4, 5] >>> tail
6 >>> 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 >>> a 1 >>> b 2 >>> c 3 >>> addr 'dddd' >>> age 10 >>> |
保留最后N个元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
>>> from collections import deque
>>> q=deque(maxlen=4) >>> q.append(1) >>> q.append(2) >>> q.append(3) >>> q deque([1, 2, 3], maxlen=4) >>> q.append(4) >>> q deque([1, 2, 3, 4], maxlen=4) >>> q.append(5) >>> q deque([2, 3, 4, 5], maxlen=4) >>> q.append(6) >>> q deque([3, 4, 5, 6], maxlen=4) >>> q.appendleft(0) >>> q deque([0, 3, 4, 5], maxlen=4) >>> q.appendleft(-1) >>> q deque([-1, 0, 3, 4], maxlen=4) >>> |
查找最大或最小的N个元素
1
2
3
4
5
6
7
8
9
10
11
|
>>> 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] >>> |
实现一个优先级队列
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
>>> 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' )
>>> |
字典中键映射多个值
1
2
3
4
5
6
7
8
9
10
11
|
>>> 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' )
>>> d defaultdict(<class 'list' >, { 'b' : [-1, -2], 'c' : [ 'C' ], 'a' : [1, 2, 3]})
>>> |
字典保持顺序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
>>> from collections import OrderedDict
>>> d=OrderedDict() >>> d[ 'B' ]=2
>>> d[ 'A' ]=1
>>> d[ 'C' ]=3
>>> d[ 'F' ]=6
>>> d[ 'E' ]=5
>>> d OrderedDict([( 'B' , 2), ( 'A' , 1), ( 'C' , 3), ( 'F' , 6), ( 'E' , 5)])
>>> for i in d:
print(i,d[i])
B 2 A 1 C 3 F 6 E 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 1 F 6 C 3 B 2 E 5 >>> |
字典的运算
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
>>> 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 >>> |
查找两个字典的相同点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
>>> 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)}
>>> |
删除序列相同元素并保持顺序
1
2
3
4
5
6
7
8
9
|
>>> def dedupe(items): seen = set ()
for item in items:
if item not in seen:
yield item seen.add(item) >>> a = [1, 5, 2, 1, 9, 1, 5, 10] >>> list(dedupe(a)) [1, 5, 2, 9, 10] |
>>>
命名切片
1
2
3
4
5
6
7
|
>>> record = '....................100 .......513.25 ..........'
>>> shares=slice(20,23) >>> price=slice(31,37) >>> cost=int(record[shares])*float(record[price]) >>> cost 51325.0 >>> |
本文转自 chaunceyjiang 51CTO博客,原文链接:http://blog.51cto.com/cqwujiang/1948892,如需转载请自行联系原作者