繁体   English   中英

如何通过将其他列表的值用作第一个列表的索引,将一个列表的值插入到另一个列表中? (Python)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM