[英]Dictionary with multiple keys in python
12245933,1418,1
12245933,1475,2
134514060,6112,3
134514064,10096,4
12245933,1536,5
...
134514097,16200,38
12245933,1475,39
I want to know for every row[0]
, the distance of re-occurance of the same value in row[1]
我想知道对于每一row[0]
, row[1]
中重新出现相同值的距离
For example: 例如:
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.
Code I tried. 我尝试过的代码。
#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
Maybe we can add row[1] as well in pc_elements dict? 也许我们也可以在pc_elements字典中添加row [1]? and get the indexes from that? 并从中获取索引?
Use tuple
s as your dictionary keys: 使用tuple
s作为您的字典键:
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]
Use nested dictionaries. 使用嵌套字典。 Map 1224953 to a dictionary which maps 1475 to a list of line numbers where the values occur. 将1224953映射到字典,该字典将1475映射到出现值的行号列表。
So your final dictionary would look like {1224953 => {1475=>[39, 2]}} 因此您的最终字典看起来像{1224953 => {1475 => [39,2]}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.