【问题标题】:Python: Subsets of Data with most common entriesPython:具有最常见条目的数据子集
【发布时间】:2015-04-30 08:56:17
【问题描述】:

我正在努力解决以下问题。 想象一下,我有很多这样的数据:

one = {'A':'m','B':'n','C':'o'}
two = {'A':'m','B':'n','C':'p'}
three = {'A':'x','B':'n','C':'p'}

等等,不一定要存储在 dicts 中。 我如何获得最常见条目的数据子集?

在上面的例子中我想得到

one, two          with same A and B = m,n
two, three        with same B and C = n,p
one, two three    with same B       = n
one, two          with same A       = m

【问题讨论】:

    标签: python numpy pandas grouping subset


    【解决方案1】:

    对于长字典来说,一种但效率不高的方法是使用itertools.combinations 查找字典之间的组合,然后循环遍历组合,然后遍历集合并获取集合项之间的交集:

    one = {'one':{'A':'m','B':'n','C':'o'}}
    two ={'two':{'A':'m','B':'n','C':'p'}}
    three = {'three':{'A':'x','B':'n','C':'p'}}
    
    dict_list=[one,two,three]
    v_item=[i.items() for i in dict_list]
    
    from itertools import combinations
    names=[]
    items=[]
    l=[combinations(v_item,i) for i in range(2,4)]
    flat=[[[t[0] for t in k] for k in j] for j in l]  
    """this line is for flattening the combinations i don't know why but python puts every elements within a list :
    >>> l
    [[([('one', {'A': 'm', 'C': 'o', 'B': 'n'})], [('two', {'A': 'm', 'C': 'p', 'B': 'n'})]), 
    ([('one', {'A': 'm', 'C': 'o', 'B': 'n'})], [('three', {'A': 'x', 'C': 'p', 'B': 'n'})]), 
    ([('two', {'A': 'm', 'C': 'p', 'B': 'n'})], [('three', {'A': 'x', 'C': 'p', 'B': 'n'})])], 
    [([('one', {'A': 'm', 'C': 'o', 'B': 'n'})], [('two', {'A': 'm', 'C': 'p', 'B': 'n'})], [('three', {'A': 'x', 'C': 'p', 'B': 'n'})])]]"""
    
    
    for comb in flat :
       for pair in comb:
         names,items =zip(*pair)
         items=[i.viewitems() for i in items]
         print names,reduce(lambda x,y:x&y,items) 
    

    结果:

    ('one', 'two') set([('B', 'n'), ('A', 'm')])
    ('one', 'three') set([('B', 'n')])
    ('two', 'three') set([('B', 'n'), ('C', 'p')])
    ('one', 'two', 'three') set([('B', 'n')])
    

    关于以下几行:

         items=[i.viewitems() for i in items]
         print names,reduce(lambda x,y:x&y,items)
    

    您需要将 create a view object of your items 视为set 对象,然后您可以计算项目与& 操作数的交集。 使用reduce 函数。

    【讨论】:

    • uff,你说得对,它在大型数据集上速度非常慢,而且内存不足 :O :/
    【解决方案2】:

    感谢 Kasra,这给了我最后的提示 :)。
    我更改了一些内容并将其转换为 Python3(忘了提...)。
    但是作为您的代码,它在大型数据集(我确实有)上非常缓慢并且内存不足。所以我必须寻找另一种方法:/。

    这是我的最终代码:

    from itertools import combinations
    from functools import reduce
    
    class Piece():
        def __init__(self,tag,A,B,C):
            self._tag = tag
            self.A = A
            self.B = B
            self.C = C
    
            self._dict = set(self.__dict__.items())  
    
    
    pieces = []
    pieces.append(Piece('one','m','n','o'))
    pieces.append(Piece('two','m','n','p'))
    pieces.append(Piece('three','x','n','p'))
    
    
    l=[combinations(pieces,i) for i in range(2,4)]
    flat =[]
    for i in l:
        for k in i:
            flat.append(k)
    
    
    for f in flat:
        print('-'*25)    
        print([j._tag for j in f])
        dicts = (i._dict for i in f)    
        matches = reduce(lambda x,y : x & y,dicts)    
        print(matches)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-22
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 2020-10-30
      • 2017-11-26
      相关资源
      最近更新 更多