Skip to content

Instantly share code, notes, and snippets.

@aur3l14no
Created May 29, 2019 04:08
Show Gist options
  • Save aur3l14no/eb5fcc3165d0b083dbaa6c6d06267dbb to your computer and use it in GitHub Desktop.
Save aur3l14no/eb5fcc3165d0b083dbaa6c6d06267dbb to your computer and use it in GitHub Desktop.
python组合数
def my_combination(arr, ns):
'''从arr中取若干组大小分别为ns[0], ns[1], ...的数据
'''
if sum(ns) != len(arr):
return
def rec(arr, ns, result):
if ns:
for subset in combinations(arr, ns[0]):
another_result = result.copy()
another_result.append(subset)
rest = [item for item in arr if item not in subset]
yield from rec(rest, ns[1:], another_result)
else:
yield result
yield from rec(arr, ns, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment