numList = [1, 3, 5, 7, 9]
from itertools import combinations
for i in range(1, len(numList) + 1): # xrange will return the values 1,2,3,4 in this loop
print(list(combinations(numList, i)))
输出:
[('1',), ('3',), ('5',), ('7',), ('9',)]
[('1', '3'), ('1', '5'), ('1', '7'), ('1', '9'), ('3', '5'), ('3', '7'), ('3', '9'), ('5', '7'), ('5', '9'), ('7', '9')]
[('1', '3', '5'), ('1', '3', '7'), ('1', '3', '9'), ('1', '5', '7'), ('1', '5', '9'), ('1', '7', '9'), ('3', '5', '7'), ('3', '5', '9')
Python获取一个列表的全组合
于 2024-06-14 10:26:57 首次发布

最低0.47元/天 解锁文章
743

被折叠的 条评论
为什么被折叠?



