我需要使用用户输入来创建笛卡尔积。我已经做到了,但是输出需要有逗号。我只是很难做到这一点。因此,如果您在ListA中输入1,2,在ListB中输入3,4,则结果应为(1,3),(1,4),(2,3),(2,4)。
当前输出如下。 ['1,3','1,4','2,3','2,4']。我确信我缺少一些非常简单的东西,只是一直想念它。
谢谢您的见解。
ListA = input("Enter up to 10 numbers seperated by commas:")
numbers = list(map(str, ListA.split(",")))
print(ListA)
ListB = input("Enter up to 10 numbers seperated by commas:")
numbers = list(map(str, ListB.split(",")))
print(ListB)
import itertools
AxB = []
for i in itertools.product(ListA.split(","), ListB.split(",")):
AxB.append(",".join(map(str, i)))
print(AxB)
答案 0 :(得分:0)
import itertools
list_a = map(int, input("Enter up to 10 numbers seperated by commas:").split(","))
list_b = map(int, input("Enter up to 10 numbers seperated by commas:").split(","))
axb = list(itertools.product(list_a, list_b))
print(axb)
似乎可以做您想做的事情:
Enter up to 10 numbers seperated by commas:1,2,3
Enter up to 10 numbers seperated by commas:4,5,6
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
(编辑:误读了原始问题,已回答...)