teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

追記

2018/10/12 07:50

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -8,6 +8,10 @@
8
8
  >>> c = list(chain.from_iterable(zip(a, b)))
9
9
  >>> c
10
10
  ['1', 'a', '2', 'b', '3', 'c']
11
+ >>>
12
+ >>> c = list(chain(*zip(a, b)))
13
+ >>> c
14
+ ['1', 'a', '2', 'b', '3', 'c']
11
15
  ```
12
16
 
13
17
  あるいはサードパーティの [more_itertools.interleave](https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.interleave) を使います。

2

実行例の変更

2018/10/12 07:50

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -2,19 +2,22 @@
2
2
  ```Python
3
3
  >>> from itertools import chain
4
4
  >>>
5
+ >>> a = list('123')
6
+ >>> b = list('abc')
7
+ >>>
5
8
  >>> c = list(chain.from_iterable(zip(a, b)))
6
9
  >>> c
7
- [1, 2, 3, 4]
10
+ ['1', 'a', '2', 'b', '3', 'c']
8
11
  ```
9
12
 
10
13
  あるいはサードパーティの [more_itertools.interleave](https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.interleave) を使います。
11
14
  ```Python
12
15
  >>> import more_itertools
13
16
  >>>
14
- >>> a = [1, 3, 5]
17
+ >>> a = list('123')
15
- >>> b = [2, 4, 6]
18
+ >>> b = list('abc')
16
19
  >>>
17
20
  >>> c = list(more_itertools.interleave(a, b))
18
21
  >>> c
19
- [1, 2, 3, 4, 5, 6]
22
+ ['1', 'a', '2', 'b', '3', 'c']
20
23
  ```

1

追記

2018/10/12 07:49

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -5,4 +5,16 @@
5
5
  >>> c = list(chain.from_iterable(zip(a, b)))
6
6
  >>> c
7
7
  [1, 2, 3, 4]
8
+ ```
9
+
10
+ あるいはサードパーティの [more_itertools.interleave](https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.interleave) を使います。
11
+ ```Python
12
+ >>> import more_itertools
13
+ >>>
14
+ >>> a = [1, 3, 5]
15
+ >>> b = [2, 4, 6]
16
+ >>>
17
+ >>> c = list(more_itertools.interleave(a, b))
18
+ >>> c
19
+ [1, 2, 3, 4, 5, 6]
8
20
  ```