在字典和Nested Array&中查找匹配键将数组替换为中的值
我也有一个类似的问题,例如 ;用价值替换键
但是我的情况看起来像这样。
d1 = {"1": "DNS Compromise", "2": "DNS Poisoning", "3": "Fraud Orders"}
d2 = {... ,"categories": [1, 2], ...}
我想拥有以下输出:
d2 = {..., "categories": [DNS Compromise, DNS Poisoning], ...}
有没有办法构造循环或使用理解?我是新手。
I have sort of a similar question as Find matching keys in dictionaries & replace keys with values
However my situation looks like this.
d1 = {"1": "DNS Compromise", "2": "DNS Poisoning", "3": "Fraud Orders"}
d2 = {... ,"categories": [1, 2], ...}
I would like to have the following output:
d2 = {..., "categories": [DNS Compromise, DNS Poisoning], ...}
Is there a way to construct a for loop or use comprehension? I am sort of new to this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用字典,您可以轻松获取使用方法
values()
的值列表。With dictionary you can easily get a list of the values with the method
values()
.这是您要搜索的解决方案:
d3 = {k:[d1 [str(v_i)] for v_i in V in V in v] for d2.items()}}
in v in v in V in v]This is the solution you are searching for :
d3 = {k:[ d1[str(v_i)] for v_i in v] for k,v in d2.items() }