979229413 发表于 2021-11-9 21:27:46

用列表查字典

dict1 = {"a":1,"b":5,"c":9}
f = ["a","b","c"]
print( for _ in f])#1,5,9

但如果f = ["a","b","c","g"]

就会报错,请问有更好的用列表查询字典的方法吗?

kogawananari 发表于 2021-11-9 21:33:15

list(map(lambda _:dict1.get(_),f))


dict1 = {"a":1,"b":5,"c":9}
f = ["a","b","c","g"]
返回

君无泪 发表于 2021-11-12 16:37:47

方法一:
dict1 = {"a":1,"b":5,"c":9}
f = ["a","b","c","g"]

print()# 如果获取的不存在时会返回一个默认值 None。
# 输出结果:

方法二:
dict1 = {"a":1,"b":5,"c":9}
f = ["a","b","c","g"]
b =
print(b)
# 输出结果:

傻眼貓咪 发表于 2021-11-12 18:24:21

不清楚你想要什么,希望对你有帮助dict1 = {"a": 1, "b": 5, "c": 9}
print(*dict1.values())1 5 9
页: [1]
查看完整版本: 用列表查字典