简体   繁体   English

有没有办法在字典中的键中获取相应的索引值?

[英]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:如果你想将这些值分配给xy你可以这样做:

>>> 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.

相关问题 获取字典中 5 个最小值对应的键 - Get the keys corresponding to the 5 smallest values within a dictionary 如何从字典列表中的字典中获取键和值 - How to get keys and values from a dictionary within a list within a dictionary 比较Python中字典的键并提取相应的值 - Comparing keys of a dictionary in Python and extracting corresponding values 在字典中找到特定的键和相应的值 - Locating specific keys and corresponding values in dictionary 从字典中删除键但添加相应的值 - Deleting keys from dictionary but adding the corresponding values 将 ASCII 键映射到相应值的字典 - Dictionary that maps ASCII keys to their corresponding values 从字典中删除具有不同键的对应值 - Delete corresponding values with different Keys from Dictionary 替换键值与对应于同一字典中的不同键的值 - python - Replace values of keys with values corresponding to different keys in the same dictionary 在python字典中查找键,以找到相应字典值中元素的交集 - Finding the keys in a python dictionary for intersection of elements in the corresponding dictionary values 如何获取与不同列表中的值相对应的python字典中所有内部级别嵌套键的列表? - How to get list of all inner level nested keys in python dictionary corresponding to values in a different list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM