删除字典python中的重复值 [英] remove duplicates values in a dictionary python

查看:107
本文介绍了删除字典python中的重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Stack Overflow周围,我发现了这个问题从字典中删除重复项另一个问题中的那个人也有我同样的问题.我尝试了他们给他的解决方案,但没有一个起作用.你能帮我吗?

I was around Stack Overflow and I find this question Removing Duplicates From Dictionary the man in the other question has my same problem. I tried the solution they give him but no of them works. Can you help me?

这是我的清单.然后是我的代码:

Here is my list. And then here is my code:

def printed(filename, day, time):
try:
    result = {}
    f = open(filename, 'r')
    lines = f.readlines()
    d = defaultdict(list)
    start = lines.index(day+"\n")
    if day == 'Monday\n':
        stop = lines.index("Saturday\n")
    elif day == 'Saturday\n':
        stop = lines.index("Sunday\n")
    else:
        stop = len(lines)
    if filename == "Bus 6 Cornaredo.txt" and filename == "Bus 6 Lugano Stazione.txt":
        if day == "Sunday":
            return "There are no buses this day for Bus 6"
    else:
        for line in lines[start:stop]:
            line = line.replace('\n','')
            line = line.replace(" ","")
            line = line.split(".")
            key = line[0]
            if len(line) == 2:
                d[key] += [line[1]]
        d = dict(d)
    for key,value in d.items():
        if value not in result.values():
            result[key] = value
    return result
except IOError:
    print("File not found")
    program()

当我在find()函数中调用"printed()"函数时,我将看到输出:

When I call the "printed()" function in the find() one, then I have the output you can see:

{'21': ['19', '49', '19', '49'],
 '16': ['17', '32', '47', '22', '52'], 
 '10': ['22', '52', '22', '52'],
 '11': ['22', '52', '22', '52'], 
 '22': ['19', '49', '19', '49'],
 '23': ['19', '49', '19', '49'], 
 '20': ['19', '49', '19', '49'],
 '17': ['03', '18', '33', '48', '22', '52'], 
 '08': ['02', '17', '32', '47', '22', '52'],
 '07': ['02', '17', '32', '47', '19', '49'], 
 '15': ['22', '52', '22', '52'],
 '13': ['22', '52', '22', '52'], 
 '09': ['02', '22', '52', '22', '52'],
 '18': ['03', '18', '33', '48', '22', '52'], 
 '14': ['22', '52', '22', '52'],
 '06': ['32', '47', '49'], 
 '12': ['22', '52', '22', '52'],
 '19': ['03', '18', '33', '49', '22', '49']}

具有相同功能的末尾有打印件.....上面是更新

Same function with a print at the end and..... Above there is the update

def print_for_day_and_hour(filename, day):
try:
    result = {}
    f = open(filename, 'r')
    lines = f.readlines()
    d = defaultdict(list)
    start = lines.index(day+"\n")
    if day == 'Monday\n':
        stop = lines.index("Saturday\n")
    elif day == 'Saturday\n':
        stop = lines.index("Sunday\n")
    else:
        stop = len(lines)
    if filename == "Bus 6 Cornaredo.txt" and filename == "Bus 6 Lugano Stazione.txt":
        if day == "Sunday":
            return "There are no buses this day for Bus 6"
    else:
        for line in lines[start:stop]:
            line = line.replace('\n','')
            line = line.replace(" ","")
            line = line.split(".")
            key = line[0]
            if len(line) == 2:
                d[key] += [line[1]]
        d = dict(d)
    for key,value in d.items():
        if value not in result.values():
            result[key] = value
    print(result)
except IOError:
    print("File not found")
    program()

这是我需要返回函数的函数:

Here is the function where I need the return function:

def find(filename, day, time):
try:
    data = printed(filename, day, time)
    data2 = [int(h) * 60 + int(m) for h in data.keys() for m in data[h]]
    start_hour, start_minute = map(int, time.split('.'))
    start = start_hour * 60 + start_minute
    end = start + 30
    after = list(filter(lambda x: start <= x <= end, data2))
    if len(after) == 0:
        return "\nThere is no bus for this time"
    return list(map(lambda x: '%02d.%02d' % (x // 60, x % 60), after))
except IOError:
    print("The file was not found")
    program()

推荐答案

您需要制作新的字典来存储结果.

You need to make a new dictionary to store your results in.

def getUniqueItems(d):
    result = {}
    for key,value in d.items():
    if value not in result.values():
        result[key] = value
    print result

这篇关于删除字典python中的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆