热线电话:13121318867

登录
2019-04-18 阅读量: 546
Python中的组合

组合

此方法将列表和输入r作为输入,并返回元组的对象列表,其中包含列表形式中长度r的所有可能组合。

# A Python program to print all

# combinations of given length

from itertools import combinations

# Get all combinations of [1, 2, 3]

# and length 2

comb = combinations([1, 2, 3], 2)

# Print the obtained combinations

for i in list(comb):

print i

输出:

(1,2)
(1,3)
(2,3)

组合以输入的字典排序顺序发出。因此,如果输入列表已排序,则组合元组将按排序顺序生成。

0.0000
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子