简体   繁体   中英

How to transform elements of list into dict/tuple? Python

I need to transform this list:

[124, '-9.6713520', '-35.745578', 132, '-9.6713765', '-35.745620', 140, '-9.6712351', '-35.745561', 159, '-9.6712457', '-35.745545']

Into:

[{'uc': 124, 'location': (-9.6713520, -35.745578)}, 
{'uc': 132, 'location': (-9.6713765, -35.745620)},
{'uc': 140, 'location': (-9.6712351, -35.745561)},
{'uc': 159, 'location': (-9.6712457, -35.745545)}]

Is there any way to do this?

If you make an iterator from your list, you can zip it with itself three times to iterate in triples:

l = [124, '-9.6713520', '-35.745578', 132, '-9.6713765', '-35.745620', 140, '-9.6712351', '-35.745561', 159, '-9.6712457', '-35.745545']

it = iter(l)   
[{'uc': k, 'location': (float(a), float(b))} for k, a, b in zip(it, it, it)]

Giving you:

[{'uc': 124, 'location': (-9.671352, -35.745578)},
 {'uc': 132, 'location': (-9.6713765, -35.74562)},
 {'uc': 140, 'location': (-9.6712351, -35.745561)},
 {'uc': 159, 'location': (-9.6712457, -35.745545)}]

You can just use a simple for loop and go through every 3 items in the list:

orig = [124, '-9.6713520', '-35.745578', 132, '-9.6713765', '-35.745620', 140, '-9.6712351', '-35.745561', 159, '-9.6712457', '-35.745545']
final = []
for i in range(0, len(orig), 3): # Note the 3 as that is the step
    new = {}
    new["uc"] = orig[i]
    new["location"] = (orig[i + 1], orig[i + 2])
    final.append(new)
print(final)

Outputs:

[{'uc': 124, 'location': ('-9.6713520', '-35.745578')}, {'uc': 132, 'location': ('-9.6713765', '-35.745620')}, {'uc': 140, 'location': ('-9.6712351', '-35.745561')}, {'uc': 159, 'location': ('-9.6712457', '-35.745545')}]

or using list comprehension:

[{"uc": orig[i], "location": (orig[i + 1], orig[i + 2])} for i in range(0, len(orig), 3)]

So you need to append data dictionaries into a new list and this could be done by looping in the list you got and since you want new addition multiples of three, we can write the loop to jump each time 3 indexes and then your addition to the new list would be happening by only one line of code.

my_list = [124, '-9.6713520', '-35.745578', 132, '-9.6713765', '-35.745620', 140, '-9.6712351', '-35.745561', 159, '-9.6712457', '-35.745545']
new_list = []
steps = 3

for i in range(0, len(my_list), steps):
  new_list.append({'uc': my_list[i], 'location': (my_list[i+1], my_list[i+2])})

print(new_list)

More variations using an iterator like Mark, but also using a map to float ( Try it online! ):

it = iter(l)   
ft = map(float, it)
result = [{'uc': uc, 'location': location}
          for uc, location in zip(it, zip(ft, ft))]
it = iter(l)   
ft = map(float, it)
keys = 'uc', 'location'
result = [dict(zip(keys, values))
          for values in zip(it, zip(ft, ft))]
from itertools import repeat
it = iter(l)   
ft = map(float, it)
result = list(map(dict, map(zip, repeat(['uc', 'location']), zip(it, zip(ft, ft)))))

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