Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中具有多个键的字典_Python_Dictionary - Fatal编程技术网

python中具有多个键的字典

python中具有多个键的字典,python,dictionary,Python,Dictionary,我想知道对于每一行[0],相同值在行[1] 例如: 12245933,1418,1 12245933,1475,2 134514060,6112,3 134514064,10096,4 12245933,1536,5 ... 134514097,16200,38 12245933,1475,39 我试过的代码 12245933 has the value 1475 in line 39 and line 2 .. i want to know all the possible occurre

我想知道对于每一行[0],相同值在
行[1]

例如:

12245933,1418,1
12245933,1475,2
134514060,6112,3
134514064,10096,4
12245933,1536,5
...
134514097,16200,38
12245933,1475,39
我试过的代码

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.

也许我们也可以在pc_元素dict中添加第[1]行?从中获取索引

使用
tuple
s作为字典键:

#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

使用嵌套字典。将1224953映射到字典,该字典将1475映射到值出现的行号列表


所以您的最终字典看起来像{1224953=>{1475=>[39,2]}

字典不能有重复的键。除了使用列表,还有其他方法吗?随着大小的增加,列表需要很长时间来处理。我假设第一列是某种类型的
id
,然后您就有了这个id的数据。您可以创建一个以id为键的dict,值将是表示数据的元组列表,即{12245933:[(1418,1),(1475,2)]etcHey您可以连接两个键。我指的是数字:other,并把它放在字典里。exmaple dict[12245933:1475]+=1这不能给出只有一个元组存在的元组吗?@pistal什么?不知道你是什么意思
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]