[英]Is there a way to get corresponding values of indexes within keys in a dictionary?
So let's say I have a dictionary for object detections:因此,假设我有一本用于对象检测的字典:
dict = {'x1y1coords': [(124, 101), (22, 104)], 'objecttype': [1, 2]}
is there a way that I can easily extract the first value under the list which is under the key value x1y1coords?有没有一种方法可以轻松提取键值 x1y1coords 下的列表下的第一个值? Just to show what I have in mind:只是为了展示我的想法:
print((dict['x1y1coords'])[0])
something like this (just to further explain what I had in mind).像这样的东西(只是为了进一步解释我的想法)。 It's like getting the index of a key index of a dictionary.这就像获取字典的键索引的索引。
And also, it is quite related to this topic, so I don't think I need to create another question (but you can advise me if you think this question needs to be discussed as another problem), I would also like to compare the values of that [x] from ['x1y1coords] to the values of [x] from ['objecttype'].而且,它与这个主题非常相关,所以我认为我不需要创建另一个问题(但如果你认为这个问题需要作为另一个问题讨论,你可以建议我),我也想比较一下来自 ['x1y1coords] 的 [x] 的值到来自 ['objecttype'] 的 [x] 的值。 By using my sample code (the one within the print function):通过使用我的示例代码(打印功能中的那个):
if (dict['objecttype'])[x] == 2:
p1, p2 = (dict['objecttype'])[x]
Yes, you can absolutely do exactly that:是的,你完全可以做到这一点:
>>> d = {'x1y1coords': [(124, 101), (22, 104)], 'objecttype': [1, 2]}
>>> d['x1y1coords']
[(124, 101), (22, 104)]
>>> d['x1y1coords'][0]
(124, 101)
If you wanted to assign those values to x
and y
you could do:如果你想将这些值分配给x
和y
你可以这样做:
>>> x, y = d['x1y1coords'][0]
>>> x
124
>>> y
101
If you wanted to do something with all the x, y
values in the list, instead of just the first one ( [0]
) you might use a for
loop:如果您想对列表中的所有x, y
值做某事,而不仅仅是第一个( [0]
),您可以使用for
循环:
>>> for x, y in d['x1y1coords']:
... print(f"x: {x}\ty: {y}")
...
x: 124 y: 101
x: 22 y: 104
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.