来自字符串列表的笛卡尔积
Cartesian product from list of Strings
要回答这个问题,您需要编写代码来生成
一个字符串列表,其中包含可以进行的所有组合
从两个输入列表中获取第一个列表中的元素,然后
紧随其后的是第二个列表的一个元素(中间有 space)。
应确定新列表中元素的顺序
主要是第一个列表中的顺序,其次
(对于具有相同第一部分的元素)按顺序
第二个名单。
因此,如果第一个输入的形式为:A, B, .. X
第二个是以下形式:1, 2, .. n
输出将是以下形式:
["A 1", "A 2", .. "An", "B 1", "B 2", .. "Bn", .. "X 1", "X 2", .. "Xn"]
(其中“..”表示中间可以有更多元素)。
注意事项:
1. 两个列表中可以有不同数量的元素。
2.如果第一个列表有M个元素,第二个列表有N个元素,
那么输出列表应该有 M*N 个元素。
""" )
print("INPUT:")
listA=[] #Creating a list
listB=[]#Creating a second list
listC=[]
while True:
print("add element y to continue")
if input()!='y':
break
else:
print("keep adding or x to break" )
while True:
if input=='x':
break
else:
input_listA = input( "Enter first comma separated list of strings: " )
print(n)
listA.append(input_listA)
print(input_listA)
print(listA)
if input=='x':
break
else:
input_listB=input( "Enter second comma separated list of int: " )
input_listB.split()
listB.append(input_listB)
break
however when i right words it for instance ["black ham ", "twice mouse", "floating cheese", "blue elf", "electric elf", "floating elf"] in the calculation for the cartesian product will be calculatin characters not words like 'b', 'l','a','c','k' how can i make the strings words inside the list and also how can i print it in the form of i mean without commas because as you can see i input delimiter along the strings
试试这个:
import itertools
a = input("Enter first comma separated list of strings: ")
b = input("Enter second comma separated list of strings: ")
result = []
for i in itertools.product(a.split(","), b.split(",")):
result.append("".join(map(str, i)))
print(result)
结果:
~ $ python3 test.py
Enter first comma separated list of strings: aa,bb
Enter second comma separated list of strings: 1,2,3
['aa1', 'aa2', 'aa3', 'bb1', 'bb2', 'bb3']
如果您希望在每对中的两个单词之间添加 space,请更改
result.append("".join(map(str, i)))
至
result.append(" ".join(map(str, i)))
输出:
['aa 1', 'aa 2', 'aa 3', 'bb 1', 'bb 2', 'bb 3']
要回答这个问题,您需要编写代码来生成 一个字符串列表,其中包含可以进行的所有组合 从两个输入列表中获取第一个列表中的元素,然后 紧随其后的是第二个列表的一个元素(中间有 space)。
应确定新列表中元素的顺序
主要是第一个列表中的顺序,其次
(对于具有相同第一部分的元素)按顺序
第二个名单。
因此,如果第一个输入的形式为:A, B, .. X
第二个是以下形式:1, 2, .. n
输出将是以下形式:
["A 1", "A 2", .. "An", "B 1", "B 2", .. "Bn", .. "X 1", "X 2", .. "Xn"]
(其中“..”表示中间可以有更多元素)。
注意事项: 1. 两个列表中可以有不同数量的元素。 2.如果第一个列表有M个元素,第二个列表有N个元素, 那么输出列表应该有 M*N 个元素。
""" )
print("INPUT:")
listA=[] #Creating a list
listB=[]#Creating a second list
listC=[]
while True:
print("add element y to continue")
if input()!='y':
break
else:
print("keep adding or x to break" )
while True:
if input=='x':
break
else:
input_listA = input( "Enter first comma separated list of strings: " )
print(n)
listA.append(input_listA)
print(input_listA)
print(listA)
if input=='x':
break
else:
input_listB=input( "Enter second comma separated list of int: " )
input_listB.split()
listB.append(input_listB)
break
however when i right words it for instance ["black ham ", "twice mouse", "floating cheese", "blue elf", "electric elf", "floating elf"] in the calculation for the cartesian product will be calculatin characters not words like 'b', 'l','a','c','k' how can i make the strings words inside the list and also how can i print it in the form of i mean without commas because as you can see i input delimiter along the strings
试试这个:
import itertools
a = input("Enter first comma separated list of strings: ")
b = input("Enter second comma separated list of strings: ")
result = []
for i in itertools.product(a.split(","), b.split(",")):
result.append("".join(map(str, i)))
print(result)
结果:
~ $ python3 test.py
Enter first comma separated list of strings: aa,bb
Enter second comma separated list of strings: 1,2,3
['aa1', 'aa2', 'aa3', 'bb1', 'bb2', 'bb3']
如果您希望在每对中的两个单词之间添加 space,请更改
result.append("".join(map(str, i)))
至
result.append(" ".join(map(str, i)))
输出:
['aa 1', 'aa 2', 'aa 3', 'bb 1', 'bb 2', 'bb 3']