简体   繁体   中英

How to merge multiple lists in python?

I have 2 lists:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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