简体   繁体   中英

remove duplicates values in a dictionary python

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM