![](/img/trans.png)
[英]How can I create a dictionary from two lists, one list is the keys, the other contains nested values?
[英]How can I insert values of one list into another by using the values of the other lists an index into the first? (Python)
假設我有兩個列表:
list1 = [[2, 4], [3], [0, 4], [1], [0, 2], []]
list2 = [12000,24000,14000,22000,13000,30000]
如何通過相應的索引位置將 list2 的值插入到 list1 中,以便輸出:
list3 = [[14000, 13000], [22000], [12000, 13000], [24000], [12000, 14000], []]
謝謝。
list3 = [[list2[y] for y in x] for x in list1]
你可以試試這個:
list1 = [[2, 4], [3], [0, 4], [1], [0, 2], []]
list2 = [12000,24000,14000,22000,13000,30000]
for i in list1:
for k,j in enumerate(i):
i[k]=list2[j]
print(list1)
輸出:
[[14000, 13000], [22000], [12000, 13000], [24000], [12000, 14000], []]
這是一個緊湊的解決方案:
List3=[[list2[x] for x in i] for i in list1]
print(list3)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.