删除字典python中的重复值

时间:2014-11-30 21:54:58

标签: python dictionary

我在Stack Overflow附近,我发现了这个问题Removing Duplicates From Dictionary 另一个问题中的男人有同样的问题。我尝试了他们给他的解决方案,但他们没有工作。你能救我吗?

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

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()中,然后我有你可以看到的输出:

{'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']}

与末尾的印刷品相同的功能......以上是更新

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

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

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

1 个答案:

答案 0 :(得分:2)

您需要制作一个新词典来存储结果。

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