Created
May 29, 2019 04:08
-
-
Save aur3l14no/eb5fcc3165d0b083dbaa6c6d06267dbb to your computer and use it in GitHub Desktop.
python组合数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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