在另一个词典中查找部分匹配词典的简单方法

2024-10-06 12:36:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行一个使用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项为假。你知道吗


Tags: youbybarnoticecompletemarkprogressnoun
1条回答
网友
1楼 · 发布于 2024-10-06 12:36:33

假设您正在寻找部分匹配(即req_item_1'pread'包含'longsword',而不是等于'longsword')。同样假设你不介意所有的比赛,你也不是在寻找最有效的方法来获得第一场比赛。还假设部分匹配意味着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是由一个列表理解构造的:

  • 迭代oncon中的所有项item
  • 对每个itemreq_item_1中的所有键k进行迭代
  • 检查键k是否在item中,并
  • 如果是,则检查req_item_1[k]的类型是否为字符串,并且是否在item键的值k中找到该类型
  • 否则,如果键kitem值与req_item_1[k]相同
  • 如果Trueitem被添加到正在构造的列表中

item_1被指定为matches中所有匹配项的第一个。你知道吗

相关问题 更多 >