簡體   English   中英

Python:map 字典鍵與列表中的值以使用 for 循環創建新列表

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

我需要使用 for 循環創建一個新字典,以便“代碼”字典中的第一個代碼等於列表“位置”中的第一個位置。 然后我需要它循環,以便第二個代碼與第二個位置匹配,等等,直到列表的末尾。

我目前正在使用下面最底部的代碼,它產生我想要的 output,但它不會創建新字典。 我很感激任何幫助! 謝謝!

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)      

我認為這就是您要尋找的東西:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM