简体   繁体   English

给定一个以字符串列表作为其值的字典,您将如何检索列表包含所有其他列表唯一的字符串的所有键?

[英]Given a dictionary with lists of strings as their values, how would you retrieve all keys where the list contains a string unique to all other lists?

As an example if the dictionary was this:例如,如果字典是这样的:

myDict = {"egg sandwich":["bread", "lettuce", "mayo","egg"], 
          "sad sandwich":["bread","lettuce","mayo"],
          "ham sandwich":["bread","lettuce","mayo","ham"],
          "healthy sandwich":["lettuce"],
          "breakfast sandwich":["bread","egg","mayo"]}

The function should return "ham sandwich" , since it is the only sandwich that contains an ingredient (ham) unique to a comparison of all other lists.该函数应返回“火腿三明治”,因为它是唯一包含与所有其他列表进行比较时所独有的成分(火腿)的三明治。

This seems to work:这似乎有效:

def get_unique_item(d):
    all_ingredients = [y for x in d.values() for y in x]

    output = []
    for name, ingredients in d.items():
        for ingredient in ingredients:
            if all_ingredients.count(ingredient) == 1:
                output.append(name)
                break

    return output

myDict = {"egg sandwich":["bread", "lettuce", "mayo","egg"], 
          "sad sandwich":["bread","lettuce","mayo"],
          "ham sandwich":["bread","lettuce","mayo","ham"],
          "healthy sandwich":["lettuce"],
          "breakfast sandwich":["bread","egg","mayo"]}


print(get_unique_item(myDict))

Output:输出:

['ham sandwich']

Basically, we create a list of all occurrences of all ingredients, and for each sandwich, we check if there are any ingredients which only occur once.基本上,我们创建一个所有成分的所有出现的列表,对于每个三明治,我们检查是否有任何成分只出现一次。


If you really want to, you can make it a one-line list comprehension:如果你真的想,你可以把它变成一个单行列表理解:

[name for name, ingredients in d.items() if any([y for x in d.values() for y in set(x)].count(i) == 1 for i in ingredients)]

暂无
暂无

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

相关问题 如何从两个列表创建字典,一个列表是键,另一个包含嵌套值? - How can I create a dictionary from two lists, one list is the keys, the other contains nested values? 将列表字典简化为包含所有/唯一唯一值的单个列表 - Simplify this dictionary of lists into a single list containing all/only unique values 从多个列表创建字典,其中每个列表没有一个键的值,但每个列表都有所有键的值 - create dictionary from multiple lists, where each list has not the values for one key, but each list has values for all keys Python字符串列表:如何从两个字符串列表的比较中检索唯一值? - Python string lists: How does one retrieve the unique values from the comparison of two lists of strings? 如何从具有列表值的字典中获取一系列键来检索所有可能的组合 - How to retrieve all possible combinations given a sequence of keys from a dictionary with list values 您将如何找到仅包含列表列表中唯一元素的列表? - How would you find a list that contains only unique elements in a list of lists? 如何将列表列表组合成字典,其中第一个子列表映射到所有后续子列表中的相应值? - How to combine a list of lists into a dictionary where the first sublist is mapped to corresponding values in all subsequent sublists? Python:遍历包含列表的字典并将键和值存储在字符串中 - Python: Iterate Over Dictionary that Contains Lists and Store the Keys and Values in a String 如何创建一个字典,其中的键是一年中所有星期的数字,值是一周中每个工作日的日期的列表? - How to create a dictionary where the keys are all the week numbers within a year and values as lists of the dates of each weekday within a week? 如何将字符串列表转换为键以给定 substring 开头的列表字典 - How to convert a list of strings to a dict of lists where keys start with given substring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM