问题
I'm experimenting with the sympy api for combinations.
First, combinations without replacement ...
from sympy.functions.combinatorial.numbers import nC
from sympy.utilities.iterables import combinations
nC('abc', 2)
# >>> 3
list(combinations('abc', 2))
# >>> [('a', 'b'), ('a', 'c'), ('b', 'c')]
I would now like to list the combinations with replacement
nC('abc', 2, replacement=True)
# >>> 6
However, the combinations() method doesn't seem to support a 'replacements' argument?
Init signature: combinations(self, /, *args, **kwargs)
Docstring:
combinations(iterable, r) --> combinations object
Return successive r-length combinations of elements in the iterable.
combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)
Type: type
回答1:
It is a different method
Init signature: sympy.utilities.iterables.combinations_with_replacement(self, /, *args, **kwargs) Docstring:
combinations_with_replacement(iterable, r) --> combinations_with_replacement objectReturn successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats. combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC Type: type
来源:https://stackoverflow.com/questions/54128074/sympy-utilities-iterables-combinations-with-replacement