将itertools combou与\u replacemens一起使用时缺少序列

2024-06-16 11:42:02 发布

您现在位置:Python中文网/ 问答频道 /正文

from itertools import combinations_with_replacement

x = 'opo'
v = combinations_with_replacement(x, len(x))
ans = [''.join(map(str, x)) for x in v]
print(" ".join(set(ans)))

我不知道为什么我错过了序列pop。为什么pop不显示,但是ppoopp显示。你知道吗

预期输出opp ppp poo ppo ooo opo oop pop

实际输出opp ppp poo ppo ooo opo oop


Tags: fromwithpopooppppjoinitertoolsreplacement
2条回答

它被正确地记录了here-

itertools.combinations_with_replacement(iterable, r)

Return r length subsequences of elements from the input iterable allowing individual elements to be repeated more than once.

Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, the generated combinations will also be unique.

我的。你知道吗

考虑一下:

>>> x = 'abc'
>>> v = itertools.combinations_with_replacement(x, len(x))
>>> ans = [''.join(map(str, x)) for x in v]
>>> ans
['aaa', 'aab', 'aac', 'abb', 'abc', 'acc', 'bbb', 'bbc', 'bcc', 'ccc']

序列中的combinations_with_replacement的作用无关;只有序列中的位置计数。你的问题和问为什么'bab''cac没有出现在我的例子中是一样的。提示:函数名不是permutations_with_replacement;-)

相关问题 更多 >