简体   繁体   English

如何在python中合并多个列表?

[英]How to merge multiple lists in python?

I have 2 lists: 我有2个清单:

c = [91.0, 92.0, 93.0, 94.0]
a = ['1,2,3,4', '1,2', '4,5,6', '']

result = [911, 912, 913, 914, 921, 922, 934, 935, 936, 94]

I tried this but still unable to get what I exactly want 我试过这个,但仍然无法得到我想要的东西

result = [x for x in zip(c,a)]

Please help me. 请帮我。

You can do it as follows: 你可以这样做:

c = [91.0, 92.0, 93.0, 94.0]
a = ['1,2,3,4', '1,2', '4,5,6', '']

c = map(str, map(int, c))

x = [int(c[k]+j) for k,i in enumerate(a) for j in i.split(',')]

>>> print x
[911, 912, 913, 914, 921, 922, 934, 935, 936, 94]

I tried to keep it readable: 我试着保持它的可读性:

C = [91.0, 92.0, 93.0, 94.0]
A = ['1,2,3,4', '1,2', '4,5,6', '']

result = []
for c, a in zip(C,A):
  str_c = str(int(c))
  nums = a.split(',')
  for num in nums:
    result.append(int(str_c + num))


print(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM