반응형
(1) Enumrate : List의 element를 추출할 때 번호를 붙여서 추출
1 2 | for i, v in enumerate(['tic', 'tac', 'toe']): print(i, v) | cs |
Output :
0 tic
1 tac
2 toe
(2) Zip : 두 개의 list의 값을 병렬적으로 추출함
1 2 3 4 5 | alist = ['a1', 'a2', 'a3'] blist = ['b1', 'b2', 'b3'] for(a, b) in zip(alist, blist): print(a, b) | cs |
Output :
a1 b1
a2 b2
a3 b3
* list + zip
1 2 3 4 | a, b, c = zip((1, 2, 3), (10, 20, 30), (100, 200, 300)) print(a, b, c) print([sum(x) for x in zip((1, 2, 3), (10, 20, 30), (100, 200, 300))]) | cs |
Output :
(1, 10, 100) (2, 20, 200) (3, 30, 300)
[111, 222, 333]
* enum + zip
1 2 3 4 5 6 7 8 | alist = ['a1', 'a2', 'a3'] blist = ['b1', 'b2', 'b3'] for(a, b) in zip(alist, blist): print(a, b) for i, (a, b ) in enumerate(zip(alist, blist)): print(i, a, b) | cs |
Output :
0 a1 b1
1 a2 b2
2 a3 b3
반응형
'Computer Science > Programming-Python' 카테고리의 다른 글
Asterisk (0) | 2019.05.06 |
---|---|
Lambda & MapReduce (0) | 2019.05.06 |
[자료구조3#] Dictionary (0) | 2019.05.06 |
[자료구조2#] Tuple (0) | 2019.05.06 |
[자료구조1#] List (0) | 2019.05.06 |
댓글