我正在运行一个使用python的MUD,我正在尝试找到一种比较和匹配两个字典的简单方法。一个是包含所需组件的部分词典,另一个是包含虚拟表或砧上所有项的大型混合词典。我想看看所需的部分字典是否在砧座上包含一个更大的字典,如果是这样,请将其定义为第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}]
如果可以找到,那么第1项应该是真的,它将成为
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'}
否则第1项为假。你知道吗
假设您正在寻找部分匹配(即
req_item_1
的'pread'
包含'longsword'
,而不是等于'longsword'
)。同样假设你不介意所有的比赛,你也不是在寻找最有效的方法来获得第一场比赛。还假设部分匹配意味着req_item_1
的所有元素都需要匹配才能被认为是好的部分匹配。你知道吗这在以下假设下起作用:
matches
是由一个列表理解构造的:oncon
中的所有项item
item
在req_item_1
中的所有键k
进行迭代k
是否在item
中,并req_item_1[k]
的类型是否为字符串,并且是否在item
键的值k
中找到该类型k
的item
值与req_item_1[k]
相同True
,item
被添加到正在构造的列表中item_1
被指定为matches
中所有匹配项的第一个。你知道吗相关问题 更多 >
编程相关推荐