我想转两个列表并通过循环每个列表中的每个字符串并连接两个用空格分隔它们来创建一个长字符串:
listA = ["a","b"]
listb = ["1","2","3"]
new_string =“a1 a2 a3 b1 b2 b3”
答案 0 :(得分:0)
试试这个:
from itertools import product
listA = ["a","b"]
listb = ["1","2","3"]
new_string = " ".join(a + b for a, b in product(listA, listb))
print(new_string)
>>> a1 a2 a3 b1 b2 b3
答案 1 :(得分:0)
这很简单
print( ' '.join([ str(i)+str(j) for i in listA for j in listB]))
答案 2 :(得分:0)
In [14]: ' '.join([' '.join(x + i for i in listb) for x in listA])
Out[14]: 'a1 a2 a3 b1 b2 b3'
答案 3 :(得分:0)
这是一个非常简单的问题解决方案,它通常在学习循环开始时讲授(至少对我而言):
listA = ["a","b"]
listb = ["1","2","3"]
new_string = ""
for i in listA:
for j in listb:
#adding a space at the end of the concatenation
new_string = new_string+i+j+" "
print(new_string)
在python 3中编码。