gpt4 book ai didi

python - 具有冗余重叠的排列? Python

转载 作者:行者123 更新时间:2023-11-30 23:34:20 28 4
gpt4 key购买 nike

我使用 itertools 在我拥有的列表上运行排列。

mylist = [a, b, c, d, e, f]
mypermutations = itertools.permutations(mylist,2)
mypermutations_list = list(mypermutations)
print mypermutations_list

打印:

[(a, b), (a, c), (a, d)...]

但是,排列列表不包括 (a, a), (b, b), 等。我认识到这可能是因为大多数人不想要这种冗余的配对。但是,我想包含这样的配对作为我正在编写的程序的控件。

有没有办法运行排列并获得这些组合?我不知道用什么来代替排列。

最佳答案

你想要itertools.product相反:

>>> import itertools
>>> mylist = ['a', 'b', 'c', 'd', 'e', 'f']
>>> list(itertools.product(mylist, repeat=2))
[('a', 'a'), ('a', 'b'), ('a', 'c'), ...]

关于python - 具有冗余重叠的排列? Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18182096/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com