我正在运行一个使用python的MUD,并且我试图找到一种比较和匹配两个字典的简便方法。一个是带有所需组件的部分字典,另一个是大型的混合字典,其中包含虚构表或砧台上的所有项目。我想看看所需的部分词典在砧上是否包含较大的词典,如果要定义为item_1。
这就是我要寻找的东西
'req_item_1':{'pread': 'longsword', 'noun': 'blade', 'complete': 1, 'progress' 0}
我想看看是否可以在其中找到包含这些项目的单个词典
'oncon':[{'hilt_type': 'longsword', 'weight': 4, 'shopdesc': 'A hefty copper bar', 'complete': 1, 'purchased_by': 20124429, 'pread': 'exquisite copper longsword', 'quality': 6, 'id': '0600ed996c854791b9d2d0aaead3edfc', 'iname': 'copper_bar', 'creator': 'Rias', 'progress': 0, 'size': 2, 'price': 1, 'crafted_by': 20124429, 'description': 'You notice nothing unusual about the hilt.', 'tags': ['hilt'], 'registered': 0, 'material': 'copper', 'heat': 0, 'postad': '', 'janitortimer': 120, 'article': 'a', 'crafting_mark': "You notice Sam's crafting mark.", 'noun': 'hilt'}, {'iname': 'copper_bar', 'weight': 4, 'shopdesc': 'A hefty copper bar', 'complete': 1, 'purchased_by': 20124429, 'pread': 'masterfully-forged copper longsword', 'quality': 7, 'id': '355bb313b3bc4b83aa1c8f7939071f53', 'size': 2, 'tags': [], 'progress': 0, 'price': 1, 'crafted_by': 20124429, 'description': 'You notice nothing unusual about the blade.', 'blade_type': 'longsword', 'registered': 0, 'material': 'copper', 'heat': 0, 'postad': '', 'article': 'a', 'crafting_mark': "You notice Sam's crafting mark.", 'noun': 'blade', 'creator': 'Rias'}, {'summoner': 20124429, 'nutrition': 10, 'noun': 'taco', 'description': 'It looks delicious!', 'weight': 0.5, 'tags': ['food'], 'material': 'food', 'deathmsg': 'A taco suddenly disappears.', 'postad': '', 'summon_duration': 27, 'pread': '', 'article': 'a', 'bites': 5, 'id': 'd52886b9505f47488e542a6fdb46f7ea', 'size': 2}]
如果可以找到,则item_1应该为true,它将变为
item_1 = {'iname': 'copper_bar', 'weight': 4, 'shopdesc': 'A hefty copper bar', 'complete': 1, 'purchased_by': 20124429, 'pread': 'masterfully-forged copper longsword', 'quality': 7, 'id': '355bb313b3bc4b83aa1c8f7939071f53', 'size': 2, 'tags': [], 'progress': 0, 'price': 1, 'crafted_by': 20124429, 'description': 'You notice nothing unusual about the blade.', 'blade_type': 'longsword', 'registered': 0, 'material': 'copper', 'heat': 0, 'postad': '', 'article': 'a', 'crafting_mark': "You notice Sam's crafting mark.", 'noun': 'blade', 'creator': 'Rias'}
否则item_1将为false。
答案 0 :(得分:0)
假设您要查找部分匹配项(即'pread'
中的req_item_1
包含 'longsword'
,而不是等价 {{1 }})。另外,假设您不介意所有比赛,那么您并不是在寻找最有效的方式来获得第一场比赛。同样假设部分匹配意味着'longsword'
的 all 个元素都必须匹配,才能被认为是良好的部分匹配。
这在以下假设下有效:
req_item_1
req_item_1 = {'pread': 'longsword', 'noun': 'blade', 'complete': 1, 'progress': 0}
oncon = [
{'hilt_type': 'longsword', 'weight': 4, 'shopdesc': 'A hefty copper bar', 'complete': 1, 'purchased_by': 20124429, 'pread': 'exquisite copper longsword', 'quality': 6, 'id': '0600ed996c854791b9d2d0aaead3edfc', 'iname': 'copper_bar', 'creator': 'Rias', 'progress': 0, 'size': 2, 'price': 1, 'crafted_by': 20124429, 'description': 'You notice nothing unusual about the hilt.', 'tags': ['hilt'], 'registered': 0, 'material': 'copper', 'heat': 0, 'postad': '', 'janitortimer': 120, 'article': 'a', 'crafting_mark': "You notice Sam's crafting mark.", 'noun': 'hilt'},
{'iname': 'copper_bar', 'weight': 4, 'shopdesc': 'A hefty copper bar', 'complete': 1, 'purchased_by': 20124429, 'pread': 'masterfully-forged copper longsword', 'quality': 7, 'id': '355bb313b3bc4b83aa1c8f7939071f53', 'size': 2, 'tags': [], 'progress': 0, 'price': 1, 'crafted_by': 20124429, 'description': 'You notice nothing unusual about the blade.', 'blade_type': 'longsword', 'registered': 0, 'material': 'copper', 'heat': 0, 'postad': '', 'article': 'a', 'crafting_mark': "You notice Sam's crafting mark.", 'noun': 'blade', 'creator': 'Rias'},
{'summoner': 20124429, 'nutrition': 10, 'noun': 'taco', 'description': 'It looks delicious!', 'weight': 0.5, 'tags': ['food'], 'material': 'food', 'deathmsg': 'A taco suddenly disappears.', 'postad': '', 'summon_duration': 27, 'pread': '', 'article': 'a', 'bites': 5, 'id': 'd52886b9505f47488e542a6fdb46f7ea', 'size': 2}
]
matches = [
item for item in oncon for k in req_item_1.keys()
if k in item and ((type(req_item_1[k]) is str and req_item_1[k] in item[k]) or
req_item_1[k] == item[k])
]
item_1 = matches[0]
print(item_1)
由以下列表理解构成:
matches
中所有item
的项目oncon
迭代k
中的所有键req_item_1
item
是否在k
中,并且item
的类型是否为字符串,并在键req_item_1[k]
的{{1}}的值中找到item
的{{1}}的值与k
相同item
,k
被添加到正在构建的列表中 req_item_1[k]
被分配了True
中所有匹配项中的第一个。