简体   繁体   中英

Merge two variables in Python

I have two variable a and b assigned some values in a list, merge those values as below

a = [{'test0': '-166.05043999999998'},
 {'test1': '0.39523979999999925'},
 {'test2': '0.8905380999999923'}]

b = [{'test3': '-168.05043999999998'},
 {'test4': '1.39523979999999925'},
 {'test5': '2.8905380999999923'}]

and I want to make it as

  ab = [{'test0': '-166.05043999999998',
 'test1': '0.39523979999999925',
 'test2': '0.8905380999999923',
 'test3': '-168.05043999999998',
 'test4': '1.39523979999999925',
 'test5': '2.8905380999999923'}]

Im not a expert in Python myself but this should work :)

a = [{'test0': '-166.05043999999998'},
 {'test1': '0.39523979999999925'},
 {'test2': '0.8905380999999923'}]

b = [{'test3': '-168.05043999999998'},
 {'test4': '1.39523979999999925'},
 {'test5': '2.8905380999999923'}]

ab = []

for i in a:
    ab.append(i)

for i in b:
    ab.append(i)

python

a = [{'test0': '-166.05043999999998'},
     {'test1': '0.39523979999999925'},
     {'test2': '0.8905380999999923'}]
b = [{'test3': '-168.05043999999998'},
    {'test4': '1.39523979999999925'},
    {'test5': '2.8905380999999923'}]
a + b

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