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

时间:2014-04-16 11:48:36

标签: python list

我有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]

我试过这个,但仍然无法得到我想要的东西

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

请帮帮我。

2 个答案:

答案 0 :(得分:4)

您可以按照以下方式执行此操作:

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]

答案 1 :(得分:1)

我试图让它保持可读性:

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)