1

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()
Community
  • 1
  • 1
pp94
  • 133
  • 5
  • 19
  • possible duplicate of [Removing Duplicates From Dictionary](http://stackoverflow.com/questions/8749158/removing-duplicates-from-dictionary) – ArtOfWarfare Nov 30 '14 at 21:57
  • 2
    What went wrong when you tried the other solution? – Luigi Nov 30 '14 at 21:58
  • @pp94 - I did read the question. That's how I was able to identify the fact that you posted a duplicate (you linked right to it). Without you giving us details as to how the prior solutions didn't help, you haven't done anything other than post a duplicate. – ArtOfWarfare Nov 30 '14 at 21:59
  • @Luigi When I tried to call this function again in a new function then I have the output you can see. If I call the function alone it works but if I call it in another one than I have what you can see – pp94 Nov 30 '14 at 22:00
  • 1
    How does the existing code not work? What do you get when you run your code? With out more details this will probably be closed as a duplicate of the question you already identified. – Andy Nov 30 '14 at 22:00
  • Please look at my update – pp94 Nov 30 '14 at 22:07
  • It's clear that the original question is not enough to solve your problem; but "I can't get it to work" is not a good question for this site since it's not going to be useful for anyone else. Instead of dumping your whole program on us, narrow down the problem and provide a (complete, runnable) minimal script with the more specific question. You might even solve your own problem that way. – alexis Nov 30 '14 at 22:27
  • Yeah it's what I'm trying to do. Basically the problem is: two identical function, one with 'return' and one with 'print'. The one with return works and the one with print doesn't – pp94 Nov 30 '14 at 22:29
  • code reduced @alexis – pp94 Nov 30 '14 at 22:32
  • Good start but still far too much. What do days of the week have to do with reducing duplicates? Make a *minimal* demonstration of the algorithm you can't get to work. Minimal amount of data too (just enough to have duplicates and non-duplicates). – alexis Nov 30 '14 at 22:46
  • Yes @alexis you are right. I put away a function but then, discussing with Sean I saw that I need it. They are two functions with one different line and the output is completely different – pp94 Nov 30 '14 at 22:48
  • Sorry to put it bluntly but that's your problem, not the reader's. If you want useful answers from experts, construct a useful question. – alexis Nov 30 '14 at 23:01
  • Did you ever see this problem? Two identical codes. One returning and one printing. Two different outputs... – pp94 Nov 30 '14 at 23:05

1 Answers1

3

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
MeetTitan
  • 3,383
  • 1
  • 13
  • 26
  • Yes, I did the same think and it works in the function where I use the return but if I use the same identical function (copy and paste) with a print instead of a return I have the output you can see above – pp94 Nov 30 '14 at 22:12
  • And I don't understand why. It is a copy and paste of the same identical code – pp94 Nov 30 '14 at 22:12
  • Are you returning a value before you run the getUniqueItems snippet? That would cause the snippet to never run. – MeetTitan Nov 30 '14 at 22:20
  • explain it better please. I don't understand anything – pp94 Nov 30 '14 at 22:21
  • If you see print_for_day_and_hour() you can see the code – pp94 Nov 30 '14 at 22:26
  • In "printed" you are not running my code, and you are returning the original dictionary d. "print_for_day_and_hour" should run as expected. – MeetTitan Nov 30 '14 at 22:34
  • I'm sorry I changed it in my code but I forget the edit – pp94 Nov 30 '14 at 22:35
  • Is there a reason you need them to be return statements? I assume you are simply printing the return statement, which is somewhat redundant. What are they returning to? – MeetTitan Nov 30 '14 at 22:40
  • I need the returning to call the function in another one. I have a find function which have to work on the output of the printed one – pp94 Nov 30 '14 at 22:42
  • I'm running out of ideas. Your code looks OK in "printed()", try debugging "find()". – MeetTitan Nov 30 '14 at 22:51
  • Do you see some errors in my find()?? I don't see anything :( I just call a function and the output has repetition. I don't think the problem is in find(). When I tried to call data = printed(filename, day, time) then the output is wrong – pp94 Nov 30 '14 at 22:54
  • I find the error can you help me if I asked an helping? – pp94 Nov 30 '14 at 23:27
  • Start a new question this one is bloated as is and isn't going anywhere productive. – MeetTitan Nov 30 '14 at 23:29