与多个键的词典在Python

pis*_*tal 2 python dictionary

12245933,1418,1
12245933,1475,2
134514060,6112,3
134514064,10096,4
12245933,1536,5
...
134514097,16200,38
12245933,1475,39
Run Code Online (Sandbox Code Playgroud)

我想知道每一个row[0],重新出现的相同价值的距离row[1]

例如:

12245933  has the value 1475 in line 39 and line 2 ..
i want to know all the possible occurrences of 1475 for 12245933 in a file.
Run Code Online (Sandbox Code Playgroud)

我试过的代码.

#datafile parser
def parse_data(file):
    pc_elements = defaultdict(list)
    addr_elements = defaultdict(list)
    with open(file, 'rb') as f:
        line_number = 0
        csvin = csv.reader((x.replace('\0','') for x in f), delimiter = ',')
        for row in csvin:
            try:
                pc_elements[int(row[0])].append(line_number)
                addr_elemets[int(row[1])].append(line_number)
                line_number += 1
            except:
                print row
                line_number += 1
                pass
Run Code Online (Sandbox Code Playgroud)

也许我们可以在pc_elements dict中添加row [1]?并从中获取索引?

zha*_*hen 5

使用tuples作为词典键:

In [63]: d='''
    ...: 12245933,1418,1
    ...: 12245933,1475,2
    ...: 134514060,6112,3
    ...: 134514064,10096,4
    ...: 12245933,1536,5
    ...: 134514097,16200,38
    ...: 12245933,1475,39
    ...: '''

In [64]: from collections import defaultdict
    ...: dic=defaultdict(list)
    ...: for l in d.split():
    ...:     tup=tuple(int(i) for i in l.split(','))
    ...:     dic[tup[:2]].append(tup[2])

In [65]: dic[(12245933, 1475)]
Out[65]: [2, 39]
Run Code Online (Sandbox Code Playgroud)