Python-如何从列表中的字典位置获得键的值

发布于 2025-01-26 12:05:22 字数 292 浏览 2 评论 0原文

如果我有以下坐标列表:

coordinateStop = [{'x':450,'y':0},{'x':630,'y':-200},{'x'' :450,'y':200},{'x':-450,'y':0},{'x':-630,'y':200},{'x':450,'y' :-200},{'x':-270,'y':-200},{'x':270,'y':-200},{'x':-630,'y':-200 }]

在列表的第一个位置,在本例450中,我将如何在字典中获取或打印字典的键'x'值?

提前致谢。

If i have a list of coordinates such as follows:

coordinatestop = [{'x': 450, 'y': 0}, {'x': 630, 'y': -200}, {'x': 450, 'y': 200}, {'x': -450, 'y': 0}, {'x': -630, 'y': 200}, {'x': 450, 'y': -200}, {'x': -270, 'y': -200}, {'x': 270, 'y': -200}, {'x': -630, 'y': -200}]

How would i go about getting or printing the value for the key 'x' of the dictionary in the first position of the list, in this case 450?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨夜星沙 2025-02-02 12:05:22

由于您的列表包含字典,因此调用任何列表的元素与调用字典相同:

>>> print(coordinatestop[0])
{'x': 450, 'y': 0}

因此,您可以将任何列表元素称为字典:

coordinatestop[0]['x']
#or
coordinatestop[0].get('x')

- >首先编写列表索引以调用字典,然后写词典的键以获取其值。

As your list contains dictionaries, calling any list's element is the same as calling a dictionary:

>>> print(coordinatestop[0])
{'x': 450, 'y': 0}

So you can refer to any list element as a dictionary:

coordinatestop[0]['x']
#or
coordinatestop[0].get('x')

--> First write the list index to call the dictionary, then the key of the dictionary to get its value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文