简体   繁体   中英

Python: map dictionary keys with values from a list to create a new list using for loop

I need to create a new dictionary by using a for loop so that the 1st code in the 'code' dictionary equals the 1st location in the list 'location'. Then I need it to loop so that the 2nd code matches with the 2nd location, etc. till the end of the list.

I'm currently using the code at the very bottom below which produces the output that I want, but it doesn't create a new dictionary. I appreciate any help! Thank you!

location = [
'In the canopy directly above our heads.',
"Between my 6 and 9 o'clock above.",
"Between my 9 and 12 o'clock above.",
"Between my 12 and 3 o'clock above.",
"Between my 3 and 6 o'clock above.",
"In a nest on the ground.",
"Right behind you."]


codes = {'111', '110', '101', '100', '011', '010', '001'}


for c, b in zip(codes, location):
   print(c, "=", b)      

I think this is what your looking for:

location = [
'In the canopy directly above our heads.',
"Between my 6 and 9 o'clock above.",
"Between my 9 and 12 o'clock above.",
"Between my 12 and 3 o'clock above.",
"Between my 3 and 6 o'clock above.",
"In a nest on the ground.",
"Right behind you."]


codes = ['111', '110', '101', '100', '011', '010', '001']
my_dict = {}

for c, b in zip(codes, location):
    my_dict[c] = b
    print(c, "=", b)

print(my_dict)

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