3
나는 n 개의 목록이있는 목록이 있다고 가정 해 봅시다. 길이가 n 인리스트의 모든 가능한 조합을 얻을 수있는 깔끔한 방법이 있는가, 각 요소는 그 위치를 유지한다.목록 목록 : 요소 조합 찾기
이lists=[[1],[2,3],[4,5]]
출력 :
[[1,2,4],
[1,3,4],
[1,2,5],
[1,3,5]]
나는 n 개의 목록이있는 목록이 있다고 가정 해 봅시다. 길이가 n 인리스트의 모든 가능한 조합을 얻을 수있는 깔끔한 방법이 있는가, 각 요소는 그 위치를 유지한다.목록 목록 : 요소 조합 찾기
이lists=[[1],[2,3],[4,5]]
출력 :
[[1,2,4],
[1,3,4],
[1,2,5],
[1,3,5]]
당신은 이것에 대한 itertools.product
를 사용할 수 있습니다
>>> import itertools
>>> lists = [[1], [2, 3], [4, 5]]
>>> list(itertools.product(*lists))
[(1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5)]
def find_permutations(lists, context):
if len(lists) == 1:
for item in lists[0]:
yield context + [item]
else:
for item in lists[0]:
for permutation in find_permutations(lists[1:], context + [item]):
yield permutation
@ F.J - 감사합니다. – root