sympy.utilities.iterables.combinations() with replacement?

北慕城南 提交于 2019-12-12 01:26:50

问题


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 object

Return 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!