简体   繁体   English

如何从Python字典值中找到常用项?

[英]How do I find common items from Python dictionary values?

Say, I have a dictionary D : 说,我有一本字典D

D = {'A': {1, 2, 3}, 'B': {2, 4, 5}, 'C': {1, 2, 7}}

Now I want to have all common items from D 's values, which would be 2. I tried to use set.intersection but it did not work out. 现在我希望得到D的值中的所有常用项,这将是2.我尝试使用set.intersection但它没有成功。

Simply, use intersection method of set : 简单地说,使用set intersection方法:

>>> set.intersection(*D.values())
{2}

D.values() will return a list of your dictionary values which are already sets, then *D.values() will unpack this list and pass it to intersection method of set class D.values()将返回已经设置的字典值列表,然后*D.values()将解压缩此列表并将其传递给set类的intersection方法

For the sake of variety, you could also use reduce() : 为了多样性,您还可以使用reduce()

>>> D = {'A': {1, 2, 3}, 'B': {2 ,4, 5}, 'C': {1, 2, 7}}
>>> reduce(lambda x, y: x & y, D.values())  # or use operator.and_ instead of lambda
{2}

reduce() is a built-in function in Python 2.x, but needs to be imported from the functools module in Python 3.x. reduce()是Python 2.x中的内置函数,但需要从Python 3.x中的functools模块导入。

If you are only looking for common values in each dictionary and not looking for a value that is in each dict then the following code will work! 如果您只是在每个字典中查找常用值而不是查找每个字典中的值,那么以下代码将起作用! Not saying that this is the fastest or the best approach, but it works! 不是说这是最快或最好的方法,但它有效! Returns a list of all duplicate values across the embedded dicts! 返回嵌入式dicts中所有重复值的列表!

def findCommon(data):

    flipped = {}
    out = []

    for i in data:
        for value in data[i]:
            if value not in flipped:
                flipped[value] = [i]
            else:
                flipped[value].append(i)
    for i in flipped:
        if len(flipped[i]) > 1:
            out.append(i)
    return out

If you go with reduce the most efficient way is to use operator.and_ 如果你使用reduce ,最有效的方法是使用operator.and_

from functools import reduce
from operator import and_

D = {'A': {1, 2, 3}, 'B': {2, 4, 5}, 'C': {1, 2, 7}}

print(reduce(and_, D.values()))

But set.intersection will be hard to beat: 但是set.intersection很难被击败:

In [89]: from functools import reduce

In [90]: from operator import and_

In [91]: timeit reduce(lambda x, y: x & y, D.values())
1000 loops, best of 3: 221 µs per loop

In [92]: timeit reduce(and_,D. values())
10000 loops, best of 3: 170 µs per loop

In [93]: timeit set.intersection(*D.values())
10000 loops, best of 3: 155 µs per loop

暂无
暂无

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

相关问题 如何将所有字典项与 python 中的公共值合并 - How to merge all dictionary items with common values in python 如何使用Python将列表中的值与字典中的常用项组合在一起? - How to combine values composed of lists with common items in a dictionary using Python? 在字典列表中查找常用字典值 - python - Find common dictionary values in list of dictionary - python 如何从python字典中提取列表中的项目 - How do I extract items which are in a list from python dictionary 如何使用嵌套字典项中存储的时间查找嵌套字典中的最新条目? (Python) - How do I find the latest entry in a nested dictionary using the time stored in nested dictionary items? (Python) 在 Python 中,给定一个包含值列表的字典,如何根据该列表中的项目数量对字典进行排序? - In Python, given a dictionary with lists in the values, how do I sort the dictionary based on the amount of items in that list? 如何从字典中删除项目 - How do I delete items from a dictionary 如何找到分组数据框中最常见的项目组合? - How do I find the most common combinations of items on a grouped dataframe? 如何在python中为具有多个值的字典查找带有值的字典键? - How to find key of dictionary with from values for a dictionary with multiple values in python? 在python中如何将字典值从字符串更改为字典? - In python how do I change dictionary values from strings into dictionaries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM