对列表理解的每个生成的实体进行操作
我正在尝试使用列表理解生成一个列表,并且我想对每个生成的实体进行操作。类似的东西:
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))
我尝试了类似的东西:
a=['1','2','3']
b=['a','b','c']
c = ",".join([x,y] for x in a for y in b)
我知道这行不通,因为 split
函数已被赋予列表列表而不是字符串列表。有没有更好的方法可以一次性做到这一点?
我试图获得的输出是 ['1a','1b','1c','2a','2b','2c','3a','3b','3c']
I am trying to generate a list using list comprehension and I want to do an operation on each generated entity. Something like:
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))
I tried something like:
a=['1','2','3']
b=['a','b','c']
c = ",".join([x,y] for x in a for y in b)
I know this won't work because the split
function has been given a list of lists instead of list of strings. Is there any better way to do this in one go?
The output I am trying to get is ['1a','1b','1c','2a','2b','2c','3a','3b','3c']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 itertools 相同:
或不使用:
或如果您有整数列表:
The same using itertools:
OR without:
OR if you have a list of integers:
或者
or