Python:将集合与字典结合使用

时间:2013-05-25 09:45:21

标签: python dictionary set

我这里有这种方法,它以字典的形式生成有向图,其中键的值是键所指向的节点,即{' stack':[' ; over',' flow']},叠加指向结束并流动......

def generateGraph(fileName):
    heroDict = {}
    graph = {}
    with open(fileName) as inFile:
        for line in inFile:#go through each line
            name_comic = line.rstrip().replace('"', '').split('\t') #split into list with name and comic book as strings
            if name_comic[1] in heroDict: #if the comic book is already in the dictionary
                heroDict[name_comic[1]] += [name_comic[0]] #add the hero into the comic's list of heroes
            else:
                heroDict.update({name_comic[1]: [name_comic[0]]}) # update dictionary with name and comic book
    for i in heroDict.values():
        for j in i:
            if graph.has_key(j):
                tempDict = copy.deepcopy(i)
                tempDict.remove(j)
                heroList = tempDict
                graph[j] += heroList
            else:
                tempDict = copy.deepcopy(i)
                tempDict.remove(j)
                heroList = tempDict
                graph[j] = heroList
        print graph #<========== the graph has duplicates, ie, values that are the same as their keys are present
    return graph

我的问题是,如何使用带字典的集合来防止将与有关密钥相同的值添加到密钥中?

1 个答案:

答案 0 :(得分:4)

以下是我重新编码图表构建器的方法;使用csv modulecollections.defaultdict class使代码更具可读性

import csv
from collections import defaultdict

def generateGraph(fileName):
    heroDict = defaultdict(list)

    with open(fileName, 'rb') as inFile:
        reader = csv.reader(inFile, delimiter='\t')
        for row in reader:
            name, comic = row[:2]
            heroDict[comic].append(name)

    graph = defaultdict(list)
    for names in heroDict.itervalues():
        for name in names:
            graph[name].extend(n for n in names if n != name)
    print graph
    return graph

此处无需使用套装。请注意,我使用了更有意义的变量名称;尽量避免使用ij,除非它们是整数索引。