简体   繁体   English

通过从嵌套字典中获取值来比较和匹配字典中的值,并从另一个字典中引用

[英]Comparing and matching values in a dictionary by getting its values from a nested dictionary taking reference from another dictionary

I'm new to Python and I'm trying to work on a problem but don't know how to proceed.我是 Python 的新手,我正在尝试解决一个问题,但不知道如何继续。 Sorry if there are any mistakes.如有错误请见谅。 You can ask me if my question is not clear.如果我的问题不清楚,你可以问我。 Even a small idea as to how to proceed would be helpful.即使是关于如何进行的一个小想法也会有所帮助。

I have a nested dictionary as-我有一个嵌套字典作为-

dicti = {0: {'Living': ['Tiger', 'Lion'],
             'NonLiving': ['Table', 'Anything']},
         1: {'Living': ['Dog', 'Animal'],
             'NonLiving': ['Cup', 'Chair']},
         2: {'Living': ['Crow', 'Pig'],
             'NonLiving': ['Home', 'Desk']},
         3: {'Living': ['Tiger', 'Lion'],
             'NonLiving': ['Cup', 'Stationery']},
         4: {'Living': ['Lion', 'Hen'],
             'NonLiving': ['Eraser', 'Pen']},
         5: {'Living': ['Animal', 'Bird'],
             'NonLiving': ['Anything', 'Anything']},
         6: {'Living': ['Bird', 'Shark'],
             'NonLiving': ['Desk', 'Table']},
         7: {'Living': ['Tiger', 'Fish'],
             'NonLiving': ['Book', 'Stationery']},
         8: {'Living': ['Crow', 'Pig'],
             'NonLiving': ['Home', 'Table']},
         9: {'Living': ['Anything', 'Animal'],
             'NonLiving': ['Stationery', 'Eraser']},
         10: {'Living': ['Fish', 'Anything'],
             'NonLiving': ['Book', 'Chair']},
         11: {'Living': ['Wolf', 'Dog'],
             'NonLiving': ['Desk', 'Table']},
         12: {'Living': ['Shark', 'Crow'],
             'NonLiving': ['Chair', 'Home']}}

I have a dictionary with labels for all the items as-我有一本带有所有项目标签的字典 -

Labels = {'Tiger': '001',
          'Pig': '002',
          'Lion': '003',
          'Dog': '004',
          'Wolf': '004',
          'Crow': '201',
          'Hen': '203',
          'Shark': '408',
          'Whale': '405',
          'Desk': '610',
          'Table': '610',
          'Chair': '719',
          'Pen': '817',
          'Book': '880',
          'Eraser': '815',
          'Tape': '525'}

Note that- Dog, Wolf and Desk, Table have same label numbers.请注意 - Dog、Wolf 和 Desk、Table 具有相同的 label 编号。

The items are grouped in another dictionary as-这些项目在另一个字典中分组为-

Groups = {'Fish': ['Shark', 'Whale'],
          'Bird': ['Crow', 'Hen'],
          'Animal': ['Tiger', 'Pig', 'Lion', 'Dog', 'Wolf'],
          'Home': ['Desk', 'Chair', 'Table'],
          'Stationery': ['Pen', 'Book', 'Eraser', 'Tape']}

Note that- The group names represent labels of all items within the group.请注意 - 组名称代表组内所有项目的标签。 ie 'Fish' will have labels 408,405.即“鱼”的标签为 408,405。 Similarly, 'Animal' will have labels 001,002,003,004同样,“动物”的标签为 001,002,003,004

I have another dictionary which is given where I want to compare the items from the dicti dictionary-我有另一本字典,我想在其中比较 dicti 字典中的项目-

Compare= {'Alpha': [[0, 1], [0, 3], [3, 4], [1, 2], [6, 12], [4, 5]],
          'Beta': [[4, 6], [3, 4], [1, 5], [6, 10], [10, 12], [2, 8]],
          'Gamma': [[0, 8], [7, 9], [3, 5], [1, 11], [1, 6], [1, 7], [5, 7]]}

The numbers 0,1,2,3...,12 in "Compare" are the keys in dicti dictionary. “比较”中的数字 0,1,2,3...,12 是 dicti 字典中的键。 I want to compare the pairs as [0,1] where 0 is compared to 1;我想将这些对作为 [0,1] 进行比较,其中 0 与 1 进行比较; [0,3] where 0 is compared to 3 and so on for all the items in Compare. [0,3] 其中 0 与 3 进行比较,以此类推,用于比较中的所有项目。 The comparison is to be done one by one first for the first elements of list 'Living' by its value in labels , then for second elements of list 'Living'.比较将首先通过其在标签中的值对列表“生活”的第一个元素进行比较,然后对列表“生活”的第二个元素进行比较。 If Living matches then move on to the First elements of list NonLiving followed by Second elements of list NonLiving.如果 Living 匹配,则移动到列表 NonLiving 的第一个元素,然后是列表 NonLiving 的第二个元素。 Every time there is not a match in any part, that pair needs to be removed from the list in "Compare".每次在任何部分没有匹配项时,都需要从“比较”中的列表中删除该对。 For example: At first while comparing the first elements in Living in Compare, the output should be-例如:首先在比较 Living in Compare 中的第一个元素时,output 应该是-

Compare = {'Alpha': [[0, 3], [4, 5]],
           'Beta': [[1, 5], [10, 12], [2, 8]],
           'Gamma': [[3, 5], [1, 11], [5, 7]]}

Note that- [4,5] is kept in Alpha and not removed as the first elements in living for these two keys are 'Lion' and 'Animal';请注意- [4,5] 保留在 Alpha 中并且没有被删除,因为这两个键的第一个元素是“Lion”和“Animal”; Lion falls under the category 'Animal' in 'Groups' dictionary.狮子属于“组”词典中的“动物”类别。 [1,5], [10,12] remains in Beta following the same logic. [1,5], [10,12] 按照相同的逻辑保持在 Beta 中。 [1,11] is kept in Gamma as the value of Dog and Wolf in 'Labels' dictionary is same. [1,11] 保存在 Gamma 中,因为“标签”字典中 Dog 和 Wolf 的值相同。

Now for the second iteration, while comparing the second elements in Living, it has to be done from this new Compare.现在对于第二次迭代,在比较 Living 中的第二个元素时,必须从这个新的 Compare 中完成。 The output should be- output 应该是-

Compare = {'Alpha': [[0, 3], [4, 5]],
           'Beta': [[10, 12], [2, 8]],
           'Gamma': [[1, 11]]}

Note that- [10,12] in Beta is kept and not removed as their second elements in Living are 'Anything' and 'Crow'.请注意,Beta 中的 [10,12] 被保留而不是被删除,因为它们在 Living 中的第二个元素是“Anything”和“Crow”。 The value 'Anything' represents all the labels. 'Anything' 值代表所有标签。 So it will always be a match irrespective of the comparison.因此,无论比较如何,它始终是匹配的。

Similarly from among the items in new Compare dictionary, we compare the first elements of 'NonLiving'.同样,在新的比较字典中的项目中,我们比较了“NonLiving”的第一个元素。 And remove the pairs that do not match.并删除不匹配的对。 And again form a new Compare dictionary by updating it.并通过更新它再次形成一个新的比较字典。 At last we compare the second elements of the 'NonLiving' key.最后我们比较'NonLiving'键的第二个元素。 The final output should be-最终的 output 应该是-

Compare = {'Alpha': [[4, 5]],
           'Beta': [[2, 8]],
           'Gamma': ''}

You can use filter with a custom filter function:您可以将filter与自定义过滤器 function 一起使用:

dicti = {0: {'Living': ['Tiger', 'Lion'], 'NonLiving': ['Table', 'Anything']}, 1: {'Living': ['Dog', 'Animal'], 'NonLiving': ['Cup', 'Chair']}, 2: {'Living': ['Crow', 'Pig'], 'NonLiving': ['Home', 'Desk']}, 3: {'Living': ['Tiger', 'Lion'], 'NonLiving': ['Cup', 'Stationery']}, 4: {'Living': ['Lion', 'Hen'], 'NonLiving': ['Eraser', 'Pen']}, 5: {'Living': ['Animal', 'Bird'], 'NonLiving': ['Anything', 'Anything']}, 6: {'Living': ['Bird', 'Shark'], 'NonLiving': ['Desk', 'Table']}, 7: {'Living': ['Tiger', 'Fish'], 'NonLiving': ['Book', 'Stationery']}, 8: {'Living': ['Crow', 'Pig'], 'NonLiving': ['Home', 'Table']}, 9: {'Living': ['Anything', 'Animal'], 'NonLiving': ['Stationery', 'Eraser']}, 10: {'Living': ['Fish', 'Anything'], 'NonLiving': ['Book', 'Chair']}, 11: {'Living': ['Wolf', 'Dog'], 'NonLiving': ['Desk', 'Table']}, 12: {'Living': ['Shark', 'Crow'], 'NonLiving': ['Chair', 'Home']}}
Labels = {'Tiger': '001', 'Pig': '002', 'Lion': '003', 'Dog': '004', 'Wolf': '004', 'Crow': '201', 'Hen': '203', 'Shark': '408', 'Whale': '405', 'Desk': '610', 'Table': '610', 'Chair': '719', 'Pen': '817', 'Book': '880', 'Eraser': '815', 'Tape': '525'}
Groups = {'Fish': ['Shark', 'Whale'], 'Bird': ['Crow', 'Hen'], 'Animal': ['Tiger', 'Pig', 'Lion', 'Dog', 'Wolf'], 'Home': ['Desk', 'Chair', 'Table'], 'Stationery': ['Pen', 'Book', 'Eraser', 'Tape']}
Compare = {'Alpha': [[0, 1], [0, 3], [3, 4], [1, 2], [6, 12], [4, 5]], 'Beta': [[4, 6], [3, 4], [1, 5], [6, 10], [10, 12], [2, 8]], 'Gamma': [[0, 8], [7, 9], [3, 5], [1, 11], [1, 6], [1, 7], [5, 7]]}
def comp(a, b):
   if 'Anything' in [a, b]:
      return True
   return ({Labels[a]} if a in Labels else {Labels.get(i, i) for i in Groups.get(a, [])})\
          &({Labels[b]} if b in Labels else {Labels.get(i, i) for i in Groups.get(b, [])})

def valid_pair(pair):
   [l1, l2], [l3, l4] = dicti[pair[0]]['Living'], dicti[pair[-1]]['Living']
   if not comp(l1, l3) or not comp(l2, l4):
      return False
   [nl1, nl2], [nl3, nl4] = dicti[pair[0]]['NonLiving'], dicti[pair[-1]]['NonLiving']
   return comp(nl1, nl3) and comp(nl2, nl4)

r = {a:k if (k:=[*filter(valid_pair, b)]) else '' for a, b in Compare.items()}

Output: Output:

{'Alpha': [[4, 5]], 'Beta': [[2, 8]], 'Gamma': ''}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM