【发布时间】:2022-01-17 09:50:50
【问题描述】:
是否可以只使用字典中的.get() 函数?这是我们能做的最好的缩短这段代码的方法吗?
n_dict 是Dict 类型(大写D)而NES 只是str 的列表
eted_info = {}
for key in n_dict:
if key in NES:
eted_info[key] = n_dict[key]
我只是好奇是否有更好/更 Pythonic 的方式来检索值,就像 C# 中的 TryGetValue。
【问题讨论】:
-
n_dict和NES是什么? -
编辑显示类型
-
大多数pythonic方式可能是使用dict理解。
-
eted_info仅包含NES中的键? ...没有空值的键?NES中有多少项?
标签: python