使用python2.7,我想生成一个新列表,输入/输出如下。想要做一个简单的方法...
输入:
l1=['1','2']
l2=['a','b','c','d']
l3=['x','y','z']
#expect output:
['1ax','1bx','1cx','2dx','1ay','1by','1cy','2dy',.......]
答案 0 :(得分:1)
使用以下代码:
l1=['1','2']
l2=['a','b','c','d']
l3=['x','y','z']
final_arr = list()
for i in l1:
for j in l2:
for k in l3:
final_arr.append('{}{}{}'.format(i,j,k))
print(final_arr)
['1ax', '1ay', '1az', '1bx', '1by', '1bz', '1cx', '1cy', '1cz', '1dx', '1dy', '1dz', '2ax', '2ay', '2az', '2bx', '2by',
'2bz', '2cx', '2cy', '2cz', '2dx', '2dy', '2dz']
答案 1 :(得分:0)
您可以将乘数更改为更远,但这会使您的数字变为偶数:
from functools import reduce
import operator
l1=['1','2']
l2=['a','b','c','d']
l3=['x','y','z']
newlist=[]
for item in zip(l1*6,l2*3,l3*4):
newlist.append(reduce(operator.add, item))
newlist
#['1ax', '2by', '1cz', '2dx', '1ay', '2bz', '1cx', '2dy', '1az', '2bx', '1cy', '2dz']