gpt4 book ai didi

python - 从内容中查找字典

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:05 26 4
gpt4 key购买 nike

我正在尝试创建一些东西来证明用左手在墙上移动穿过迷宫的概念是可行的,所以我制作了一个 python 3.6 turtle 程序来完成它。这是非常低效的,我知道,我是初学者,但我的问题是;如何从字典的内容中找到字典的名称。

这是一个例子:

place1 = {"coordinates" : (150, 150), "filled_in": False}
place2 = {"coordinates" : (100, 100), "filled_in": True}
place3 = {"coordinates" : (50, 50), "filled_in": True}
turtle position = 50, 50

本质上是这样的

?["坐标":(50, 50), "filled_in"= True

我希望能够从其中的值中找到字典,这样我就可以检查它是否被填充了。

if (dictionary containing value of "coordinates" : (50, 50))["filled_in"] = False:
do whatever

我知道我的格式可能有误,但在此先感谢您的帮助。

最佳答案

你可以将它们全部放在一个可以通过坐标索引的字典中:

place1 = {"coordinates" : (150, 150), "filled_in": False}
place2 = {"coordinates" : (100, 100), "filled_in": True}
place3 = {"coordinates" : (50, 50), "filled_in": True}
places = {p["coordinates"]: p for p in [place1, place2, place3]}

然后只是索引它:

>>> places[(50, 50)]['filled_in']
True
>>> places[(150, 150)]['filled_in']
False

关于python - 从内容中查找字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46051428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com