使用元组作为字典键

Ghi*_*ADJ 1 python dictionary

我使用GraphViz库,在某些情况下,它返回一个以元组为键的字典.

{(4, 7): 0, (2, 6): 1, (10, 4): 1, (5, 11): 1, (4, 5): 1,
(2, 8): 0, (8, 11): 0, (10, 0): 1, (6, 11): 1, (9, 11): 1,
(1, 9): 0, (10, 1): 0, (7, 11): 1, (0, 9): 1, (3, 7): 1,
(10, 3): 1, (10, 2): 1}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我想得到元组的第二个数字:第一个数字== 10 , 值== 1

我试过访问字典,(10, )但我认为python中不允许使用这种语法.

答案应该是: [4 ,0 ,3 , 2]

Lev*_*sky 6

你将不得不迭代字典,例如:

In [1]: d = {(4, 7): 0, (2, 6): 1, (10, 4): 1, (5, 11): 1, (4, 5): 1,
   ...: (2, 8): 0, (8, 11): 0, (10, 0): 1, (6, 11): 1, (9, 11): 1,
   ...: (1, 9): 0, (10, 1): 0, (7, 11): 1, (0, 9): 1, (3, 7): 1,
   ...: (10, 3): 1, (10, 2): 1}

In [2]: [b for (a, b), v in d.items() if a == 10 and v == 1]
Out[2]: [4, 0, 3, 2]
Run Code Online (Sandbox Code Playgroud)