【发布时间】:2019-03-11 06:07:02
【问题描述】:
我有两个字典:(1)Inventory and (2)Items
在这些字典下是使用用户输入添加的元组。
dict_inventory = {('fruits', ['apple','mango'])
dict_items = {('apple', [3, 5])
('mango', [4, 6])}
如何比较两者并匹配apple 和mango 的相似值
我的代码没有打印任何东西:
for itemA in dict_inventory.values():
for itemB in dict_items.keys():
if itemA == itemB:
print("Match!")
【问题讨论】:
-
请准确定义您的字典,例如
print(dict_inventory)。如果不了解这些是如何定义的,我们很难提供帮助。 -
你需要使用两个字典吗?您可以将它们组合成一个嵌套字典。
-
@jpp 我一周前开始使用 Python。我真的不确定你的意思,但这些词典也是使用用户输入添加的。但我会编辑帖子并添加我认为您可能需要的代码。
-
@Tanmayjain 是的。
-
你能编辑你的问题并放入样本(1)库存和(2)项目字典
标签: python python-3.x dictionary tuples