gpt4 book ai didi

python - 具有固定长度的元组的子集

转载 作者:行者123 更新时间:2023-12-01 05:16:37 25 4
gpt4 key购买 nike

我希望有一个函数 subset(("A","b","C","D"),3),它给出以下输出:

("A","b","C")
("A","b","D")
("A","C","D")
("b","C","D")

我如何在 python 3 中执行此操作?

最佳答案

itertools.combinations 函数是专门为此目的构建的:

>>> from itertools import combinations
>>> list(combinations(("A","b","C","D"), 3))
[('A', 'b', 'C'), ('A', 'b', 'D'), ('A', 'C', 'D'), ('b', 'C', 'D')]
>>>

来自docs :

itertools.combinations(iterable, r)

Return r length subsequences of elements from the input iterable.

关于python - 具有固定长度的元组的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23064314/

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