10

朋友们,我有一个字典列表:

my_list = 
[
{'oranges':'big','apples':'green'},
{'oranges':'big','apples':'green','bananas':'fresh'},
{'oranges':'big','apples':'red'},
{'oranges':'big','apples':'green','bananas':'rotten'}
]

我想创建一个新列表,消除部分重复。

就我而言,必须删除这本字典:

{'oranges':'big','apples':'green'}

,因为它复制了更长的字典:

{'oranges':'big','apples':'green','bananas':'fresh'}
{'oranges':'big','apples':'green','bananas':'rotten'}

因此,期望的结果:

[
{'oranges':'big','apples':'green','bananas':'fresh'},
{'oranges':'big','apples':'red'},
{'oranges':'big','apples':'green','bananas':'rotten'}
]

怎么做?太感谢了!

4

5 回答 5

5

想到的第一个[嗯,第二个,有一些编辑..]是这样的:

def get_superdicts(dictlist):
    superdicts = []
    for d in sorted(dictlist, key=len, reverse=True):
        fd = set(d.items())
        if not any(fd <= k for k in superdicts):
            superdicts.append(fd)
    new_dlist = map(dict, superdicts)
    return new_dlist

这使:

>>> a = [{'apples': 'green', 'oranges': 'big'}, {'apples': 'green', 'oranges': 'big', 'bananas': 'fresh'}, {'apples': 'red', 'oranges': 'big'}, {'apples': 'green', 'oranges': 'big', 'bananas': 'rotten'}]
>>> 
>>> get_superdicts(a)
[{'apples': 'red', 'oranges': 'big'}, 
 {'apples': 'green', 'oranges': 'big', 'bananas': 'rotten'}, 
 {'bananas': 'fresh', 'oranges': 'big', 'apples': 'green'}]

[本来我frozenset这里用的是a,以为可以做一些巧妙的set操作,但显然什么都没想出来。]

于 2012-12-28T07:26:36.510 回答
3

尝试以下实现

请注意,在我的实现中,我只对 2 对组合进行预排序和选择以减少迭代次数。这将确保密钥的大小始终小于或等于干草

>>> my_list =[
{'oranges':'big','apples':'green'},
{'oranges':'big','apples':'green','bananas':'fresh'},
{'oranges':'big','apples':'red'},
{'oranges':'big','apples':'green','bananas':'rotten'}
]

#Create a function remove_dup, name it anything you want
def remove_dup(lst):
    #import combinations for itertools, mainly to avoid multiple nested loops
    from itertools import combinations
    #Create a generator function dup_gen, name it anything you want
    def dup_gen(lst):
        #Now read the dict pairs, remember key is always shorter than hay in length
        for key, hay in combinations(lst, 2):
            #if key is in hay then set(key) - set(hay) = empty set
            if not set(key) - set(hay):
                #and if key is in hay, yield it
                yield key
    #sort the list of dict based on lengths after converting to a item tuple pairs
    #Handle duplicate elements, thanks to DSM for pointing out this boundary case
    #remove_dup([{1:2}, {1:2}]) == []
    lst = sorted(set(tuple(e.items()) for e in lst), key = len)
    #Now recreate the dictionary from the set difference of
    #the original list and the elements generated by dup_gen
    #Elements generated by dup_gen are the duplicates that needs to be removed
    return [dict(e) for e in set(lst) - set(dup_gen(lst))]

remove_dup(my_list)
[{'apples': 'green', 'oranges': 'big', 'bananas': 'fresh'}, {'apples': 'green', 'oranges': 'big', 'bananas': 'rotten'}, {'apples': 'red', 'oranges': 'big'}]

remove_dup([{1:2}, {1:2}])
[{1: 2}]

remove_dup([{1:2}])
[{1: 2}]

remove_dup([])
[]

remove_dup([{1:2}, {1:3}])
[{1: 2}, {1: 3}]

更快的实施

def remove_dup(lst):
    #sort the list of dict based on lengths after converting to a item tuple pairs
    #Handle duplicate elements, thanks to DSM for pointing out this boundary case
    #remove_dup([{1:2}, {1:2}]) == []
    lst = sorted(set(tuple(e.items()) for e in lst), key = len)
        #Generate all the duplicates
    dups = (key for key, hay in combinations(lst, 2) if not set(key).difference(hay))
    #Now recreate the dictionary from the set difference of
    #the original list and the duplicate elements
    return [dict(e) for e in set(lst).difference(dups)]
于 2012-12-28T07:11:10.200 回答
2

这是您可以使用的一种实现:-

>>> my_list = [
{'oranges':'big','apples':'green'},
{'oranges':'big','apples':'green','bananas':'fresh'},
{'oranges':'big','apples':'red'},
{'oranges':'big','apples':'green','bananas':'rotten'}
]

>>> def is_subset(d1, d2):
        return all(item in d2.items() for item in d1.items())
        # or
        # return set(d1.items()).issubset(set(d2.items()))

>>> [d for d in my_list if not any(is_subset(d, d1) for d1 in my_list if d1 != d)]
[{'apples': 'green', 'oranges': 'big', 'bananas': 'fresh'}, 
 {'apples': 'red', 'oranges': 'big'}, 
 {'apples': 'green', 'oranges': 'big', 'bananas': 'rotten'}]

d对于每个字典my_list: -

any(is_subset(d, d1) for d1 in my_list if d1 != d)

检查它是否dictmy_list. 如果它返回True,则至少有一个 dict ,其子集是d。因此,我们将其中的一个从列表not中排除。d

于 2012-12-28T07:16:11.130 回答
1

简短的回答

def is_subset(d1, d2):
    # Check if d1 is subset of d2
    return all(item in d2.items() for item in d1.items())

filter(lambda x: len(filter(lambda y: is_subset(x, y), my_list)) == 1, my_list)
于 2012-12-28T07:22:33.600 回答
1

我认为它有更好的时间顺序:

def is_subset(a, b):
    return not set(a) - set(b)

def remove_extra(my_list):
    my_list = [d.items() for d in my_list]
    my_list.sort()

    result = []
    for i in range(len(my_list) - 1):
        if not is_subset(my_list[i], my_list[i + 1]):
            result.append(dict(my_list[i]))
    result.append(dict(my_list[-1]))

    return result

print remove_extra([
        {'oranges':'big','apples':'green'},
        {'oranges':'big','apples':'green','bananas':'fresh'},
        {'oranges':'big','apples':'red'},
        {'oranges':'big','apples':'green','bananas':'rotten'}
    ])
于 2012-12-28T07:27:44.543 回答