简体   繁体   English

Python:根据单独字典中设置的类型,将键与字典中每个键对值的值进行比较

[英]Python: Compare the key against the values of each key pair values in a dictionary based on the type set in a separate dictionary

Given two separate example dictionaries: 给定两个单独的示例字典:

fruit_type = {'apple': 'stonefruit', 'peach': 'stonefruit', 'pear': 'stonefruit','orange': 'citrus', 'lemon': 'citrus', 'tangerine': 'citrus'}
similar_fruit = {'apple': ['peach', 'pear'], 'peach': ['apple', 'pear'], 'pear': ['apple', 'peach'], 'orange': ['lemon', 'tangerine'], 'lemon': ['orange', 'tangerine'], 'tangerine': ['orange', 'lemon']}

What is the most efficient method of comparing each of the keys against the values in the similar_fruit dictionary based on their values in the other dictionary fruit_type ? 根据另一个字典fruit_type中的键值将similar_fruit键中的每个键与similar_fruit键中的值进行比较的最有效方法是什么?

I have what seems like a naive working implementation below: 我在下面似乎是一个天真的工作实现:

same_type = False

for fruit in similiar_fruit:
  for comparison in similiar_fruit[fruit]:
    if fruit_type[fruit] == fruit_type[comparison]:
      same_type = True

It might be helpful to note that there are only 2 types of fruits at all times (stonefruit, citrus), and the keys in the first dictionary line up with the keys in the second dictionary in terms of same keys (apple, peach, pear, orange, lemon, tangerine), not necessarily the order of the keys. 可能总是有两种水果类型(核果,柑橘),并且第一个字典中的键与第二个字典中的键就相同的键(苹果,桃子,梨)对齐可能会有所帮助,橘子,柠檬,橘子),不一定是键的顺序。

Thank you in advance. 先感谢您。

I don't believe there is any other way to simplify that! 我认为没有其他方法可以简化! However, I would make the change of starting same_type = True , and make the comparison as: 但是,我将更改起始same_type = True ,并进行如下比较:

if fruit_type[fruit] != fruit_type[comparison]:
  same_type = False
  break

That way as soon as a comparison is incorrect, it will fail the inner clause to prevent comparison overwriting, and stop the loop. 这样,一旦比较不正确,它将使内部子句失败以防止比较覆盖,并停止循环。

Also, if you were looking to simply create a dictionary of each fruit and make a list of which ones they are similar to rather than starting with two dictionaries to compare, I wrote a code to accomplish that here: 另外,如果您只是想简单地为每个水果创建一个字典,并列出它们与之相似的列表,而不是先将两个字典进行比较,那么我在此处编写了代码来完成该工作:

fruit_type = {'apple': 'stonefruit', 'peach': 'stonefruit', 'pear': 'stonefruit','orange': 'citrus', 'lemon': 'citrus', 'tangerine': 'citrus'}
similar_fruits = {}

for fruit in fruit_type:
  similar = []
  for comp in fruit_type:
    if fruit != comp:
      if fruit_type[fruit] == fruit_type[comp]:
        similar.append(comp)
  similar_fruits[fruit] = similar

print(similar_fruits)

similar_fruits will then be an automatic dictionary of all fruits and lists of each fruit they are similar to. similar_fruits将是所有水果及其相似的每个水果的列表的自动词典。 {'apple': ['peach', 'pear'], 'peach': ['apple', 'pear'], 'pear': ['apple', 'peach'], 'orange': ['lemon', 'tangerine'], 'lemon': ['orange', 'tangerine'], 'tangerine': ['orange', 'lemon']}

Hope this helps! 希望这可以帮助! -Nate -Nate

Then pair up each fruit with every similar fruit: 然后将每个水果与每个相似的水果配对:

similar_pairs = [(f,sim) for f,sims in similar_fruit.items() for sim in sims]

Now ask if both members of the pair are in the same type: 现在,请问这对成员是否都属于同一类型:

for a,b in similar_pairs:
    if fruit_type[a] == fruit_type[b]:
        print(f"{a} and {b} are similar and the same kind")

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

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