回答編集履歴

1

コメント欄の質問について、追加回答

2017/05/08 17:37

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -25,3 +25,37 @@
25
25
 
26
26
 
27
27
  ```
28
+
29
+ ---
30
+
31
+ > zip(*ab)はどういうことを行っているのですか?
32
+
33
+
34
+
35
+ 1, [*演算子](https://docs.python.jp/3/tutorial/controlflow.html#tut-unpacking-arguments)を使ってlistをアンパックします。
36
+
37
+ ```python
38
+
39
+ print(*ab)
40
+
41
+ # output
42
+
43
+ ('a1', 'b1') ('a2', 'b2') ('a3', 'b3') ('an', 'bn')
44
+
45
+ ```
46
+
47
+ 2,[zip関数](https://docs.python.jp/3/library/functions.html#zip)は引数で渡されたそれぞれの要素を集めたtupleのイテレータを作ります。
48
+
49
+ ```python
50
+
51
+ for i in zip(('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3'), ('an', 'bn')):
52
+
53
+ print(i)
54
+
55
+ # output
56
+
57
+ ('a1', 'a2', 'a3', 'an')
58
+
59
+ ('b1', 'b2', 'b3', 'bn')
60
+
61
+ ```