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

回答編集履歴

1

追記

2017/11/04 03:53

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -5,4 +5,18 @@
5
5
  indices = [i for i, x in enumerate(hoge) if x == 1] + [len(hoge)]
6
6
  result = [hoge[begin:end] for begin, end in zip(indices, indices[1:])]
7
7
  print(result)
8
+ ```
9
+
10
+ ---
11
+ ちょっと改造。ほぼ変わりませんが。
12
+ ```Python
13
+ def get_indices(arg, num):
14
+ for i, v in enumerate(arg):
15
+ if v == num:
16
+ yield i
17
+
18
+ hoge = [1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 1, 2, 3, 4, 5, 6]
19
+ indices = list(get_indices(hoge+[1], 1))
20
+ result = [hoge[begin:end] for begin, end in zip(indices, indices[1:])]
21
+ print(result)
8
22
  ```