打印不带括号和逗号的列表(python)

时间:2020-09-30 14:45:43

标签: python

我需要列表帮助。 这是代码:(此程序形成了您所输入的3位数字的5种组合)

a, b, c = input("Dati 3 cifre, separate prin spatiu (1xSpace) \t").split(" ")
list1 = [a]
list2 = [b]
list3 = [c]
comb1 = list1 + list2 + list3
comb2 = list1 + list3 + list2
comb3 = list2 + list1 + list3
comb4 = list2 + list3 + list1
comb5 = list3 + list2 + list1
list_final = [comb1, comb2, comb3, comb4, comb5]
print(list_final)

如您所见,我创建了一些列表,其中所有列表汇总为一个大列表。但是程序需要打印这些列表,而没有任何方括号和逗号,只打印数字。

1 个答案:

答案 0 :(得分:1)

首先,如果您可以使用itertools.permutations,则看起来您正在手动尝试计算所有可能的排列。之后,要显示没有大括号或逗号的值(这就是python本地显示列表的方式),您可以遍历并手动打印每个值。

from itertools import permutations
a = 1
b = 2
c = 3

for i,j,k in permutations([a,b,c]):
    print(i, j, k)

输出

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1