list
をループして、現在の要素を他の要素から分離したい。このような :
for e in the_list:
function_call(e, <the_list but e>)
それを行うエレガントな方法はありますか?
list
をループして、現在の要素を他の要素から分離したい。このような :
for e in the_list:
function_call(e, <the_list but e>)
それを行うエレガントな方法はありますか?
リストを使用enumerate
してスライスすることができます:
for index, elem in enumerate(the_list):
function_call(elem, the_list[:index] + the_list[index + 1:])
(合理的に)よく読み、インデックスをいじる必要のない優れたソリューション。
>>> from itertools import combinations
>>> data = [1, 2, 3, 4]
>>> for item, rest in zip(data,
reversed(list(combinations(data, len(data)-1)))):
... print(item, rest)
...
1 (2, 3, 4)
2 (1, 3, 4)
3 (1, 2, 4)
4 (1, 2, 3)