小编Sad*_*Dip的帖子

生成列表中元素的所有可能组合

我一直在尝试创建一个脚本,其中将打印列表的每个可能组合[其中 (1,2) 和 (2,1) 将被视为不同的条目]。例如:

c = [1,2]
# do something magical
print(c with magical stuff) 
>>>[(1), (2), (1, 1), (1, 2), (2, 1), (2, 2)]
Run Code Online (Sandbox Code Playgroud)

我试过 itertools.permutations。它显示输出为 >>> () (1,) (​​2,) (1, 2) (2, 1)。但是,它不包括 (1, 1) 和 (2,2)

任何帮助将不胜感激。我是编码新手(我非常流利地打印“Hello World!”虽然:3)

python combinations tuples list python-itertools

1
推荐指数
1
解决办法
2050
查看次数

标签 统计

combinations ×1

list ×1

python ×1

python-itertools ×1

tuples ×1