gpt4 book ai didi

python - 操作列表中字典中的键/项目

转载 作者:行者123 更新时间:2023-11-30 22:36:21 25 4
gpt4 key购买 nike

我发现很难操作列表中字典内的键和项目。我想在变量中获取,例如,列表中所有字典中第一个索引键中的第一个索引项:

Dict = [{"top": 1, "bottom": "a", "left": "b"}, {"top": 2, "bottom": "c", "left": "d"}, {"top": 3, "bottom": "e", "left": "sdfasda"}, {"top": 4, "bottom": "f", "left": "g"}]

所需输出:

[1, 2, 3, 4] *#All part of the key "top"*

[a, c, e, f] *#All part of the key "bottom"*

取决于我需要哪个 key 。

我本以为:

for x in Dict:
print(x("top"))

非常感谢您的帮助..

最佳答案

字典值是通过索引运算符[key]获取的,而不是通过(key)获取的。最后一个用于调用可调用对象的调用。

[x["top"] for x in Dict]

就可以了。

关于python - 操作列表中字典中的键/项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44203876/

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