从字典中的每个值中删除重复项

时间:2016-03-03 13:47:03

标签: python dictionary

我有一个包含列表列表的字典,如下所示:

{'S26': [['2016-03-18', '2016-03-28'], ['2016-03-18', '2016-03-28']], 'S24': [['2016-03-19', '2016-03-25'], ['2016-03-25', '2016-04-03']], 'S25': [['2016-03-18', '2016-03-25'], ['2016-03-18', '2016-03-25'], ['2016-03-20', '2016-03-25'], ['2016-03-20', '2016-03-25'], ['2016-03-25', '2016-04-03']]}

我想浏览每个列表并删除任何重复的子列表。因此,例如,键S26在列表中有两个重复:

'S26': [['2016-03-18', '2016-03-28'], ['2016-03-18', '2016-03-28']]

此密钥现在应为:

'S26': [['2016-03-18', '2016-03-28']]

这可能吗?该系统上的python版本是2.6.6

4 个答案:

答案 0 :(得分:1)

for val in dict:
    tmp=[]
    for l in dict[val]:
        if l not in tmp: tmp.append(l)
    dict[val]=tmp

答案 1 :(得分:1)

这个也适用:

>>> from collections import defaultdict
>>>
>>> d = {'S26': [['2016-03-18', '2016-03-28'], ['2016-03-18', '2016-03-28']], 'S24': [['2016-03-19', '2016-03-25'], ['2016-03-25', '2016-04-03']], 'S25': [['2016-03-18', '2016-03-25'], ['2016-03-18', '2016-03-25'], ['2016-03-20', '2016-03-25'], ['2016-03-20', '2016-03-25'], ['2016-03-25', '2016-04-03']]}
>>> 
>>> out_d = defaultdict(list)
>>> for k,v in d.items():
        for vv in v:
            if vv not in out_d[k]:
                out_d[k].append(vv)

>>> out_d
defaultdict(<class 'list'>, {'S24': [['2016-03-19', '2016-03-25'], ['2016-03-25', '2016-04-03']], 'S26': [['2016-03-18', '2016-03-28']], 'S25': [['2016-03-18', '2016-03-25'], ['2016-03-20', '2016-03-25'], ['2016-03-25', '2016-04-03']]})

答案 2 :(得分:0)

您可以使用以下内容:

tnsnames.ora

<强>输出

.exe

这将迭代字典中的每个d = {'S26': [['2016-03-18', '2016-03-28'], ['2016-03-18', '2016-03-28']], 'S24': [['2016-03-19', '2016-03-25'], ['2016-03-25', '2016-04-03']], 'S25': [['2016-03-18', '2016-03-25'], ['2016-03-18', '2016-03-25'], ['2016-03-20', '2016-03-25'], ['2016-03-20', '2016-03-25'], ['2016-03-25', '2016-04-03']]} answer = {k:[list(el) for el in set([tuple(sublist) for sublist in v])] for k, v in d.items()} print(answer) 元素对,并从列表值中删除重复的子列表。为此,我们使用列表推导将每个{'S24': [['2016-03-19', '2016-03-25'], ['2016-03-25', '2016-04-03']], 'S25': [['2016-03-20', '2016-03-25'], ['2016-03-18', '2016-03-25'], ['2016-03-25', '2016-04-03']], 'S26': [['2016-03-18', '2016-03-28']]} 转换为(k, v),以便我们可以创建sublist(因为列表不可用)。

答案 3 :(得分:0)

如果您需要保留列表的顺序,可以使用以下方法:

from collections import OrderedDict

dlol = {
    'S26': [['2016-03-18', '2016-03-28'], ['2016-03-18', '2016-03-28']], 
    'S24': [['2016-03-19', '2016-03-25'], ['2016-03-25', '2016-04-03']], 
    'S25': [['2016-03-18', '2016-03-25'], ['2016-03-18', '2016-03-25'], ['2016-03-20', '2016-03-25'], ['2016-03-20', '2016-03-25'], ['2016-03-25', '2016-04-03']]}

output = {}

for k, lol in dlol.items():
    output[k] = OrderedDict([(tuple(e), None) for e in lol])

output = {k: list(list(e) for e in v) for k, v in output.items()}    
print(output)

给你:

{'S25': [['2016-03-18', '2016-03-25'], ['2016-03-20', '2016-03-25'], ['2016-03-25', '2016-04-03']], 
'S24': [['2016-03-19', '2016-03-25'], ['2016-03-25', '2016-04-03']], 
'S26': [['2016-03-18', '2016-03-28']]}