Gur*_*sad 1 python string split list-comprehension
我试图使用列表理解生成一个列表,我想对每个生成的实体进行操作.就像是:
a=['1','2','3']
b=['a','b','c']
temp = [[x,y] for x in a for y in b]
c=[]
for t in temp:
c.append("".join(t))
Run Code Online (Sandbox Code Playgroud)
我尝试过类似的东西:
a=['1','2','3']
b=['a','b','c']
c = ",".join([x,y] for x in a for y in b)
Run Code Online (Sandbox Code Playgroud)
我知道这不起作用,因为split函数已经给出了列表而不是字符串列表.有没有更好的方法一次性完成这项工作?
我想要的输出是 ['1a','1b','1c','2a','2b','2c','3a','3b','3c']
>>> [x + y for x in a for y in b]
['1a', '1b', '1c', '2a', '2b', '2c', '3a', '3b', '3c']
Run Code Online (Sandbox Code Playgroud)