【发布时间】:2020-05-16 06:32:15
【问题描述】:
我有这本词典:
Dict = {
"a" : 1,
"b" : 2,
"c" : 3
}
还有这两个列表:
List1 = ["a","c"]
List2 = [0]
有没有比以下方法更有效的方法将 List1 通过 Dict 的相应值附加到 List2 ? :
for e in List1:
List2.append(Dict[e])
结果:
[0, 1, 3]
【问题讨论】:
-
你可以从字典中获取值:dict.values()。它会返回列表中的所有值
-
list2.extend(Dict[e] for e in List1)?
标签: python list dictionary for-loop append