【发布时间】:2018-04-03 13:25:35
【问题描述】:
您好,我只是想知道我们如何计算笛卡尔积(intertools.product(*somelist)) 但不打印逗号?
我想打印这样的东西!
So, if the first input is of the form: A, B, .. X
and the second is of the form: 1, 2, .. n
the output would be of the form:
["A 1", "A 2", .. "An", "B 1", "B 2", .. "Bn", .. "X 1", "X 2", .. "Xn"]
(where the ".." indicates there can be more elements in between).
Here is a more specific case:
For inputs: blue, electric, floating
and: cheese, eel
we would get:
["blue cheese", "electric cheese", "floating cheese", "blue eel",...........
HOWEVER WHEN I DO
listA=[] #Creating a list
listB=[]#Creating a second list
listC=[]
input_listA=input( "Enter second comma separated list of int: " )
listA.append(input_listA)
input_listB = input( "Enter first comma separated list of strings: " )
listB.append(input_listB)
listC.append(listB)
listC.append(listA)
for i in itertools.product(*listA,*listB):
print(i)
它给出这个输出:
INPUT:输入第二个逗号分隔的 int 列表:A、B、C 输入第一个逗号分隔的字符串列表: 1, 2, 3 ('A', '1') ('A', ',') ('A', ' ') ('A', '2') ('A', ',') ('A', '') ('A', '3') (',', '1') (',' , ',') (',', '') (',', '2') (',', ',') (',', '') (',', '3') ('', '1' ) (' ', ',') (' ', ' ') (' ', '2') (' ', ',') (' ', ' ') (' ', '3') ('B', '1') ('B', ',') ('B', '')
我怎样才能以“A 1”、“A 2”、..“An”的正确形式打印它们 如何删除“,”和“”
【问题讨论】:
标签: python python-3.x delimiter itertools cartesian-product