繁体   English   中英

如何在索引 python 中获取字典键

[英]how to get dictionary key inside index python

我有一个格式像这样的数据

data_lists = [{'x': '1', 'y': '2', 'z': 3}]

如果我想得到'x'或'y'怎么办?

我只是尝试data_lists[{'x'}] ,但出现错误('list indices must be integers or slices, not set',)

也与data_lists[{[0]}] ,错误说("unhashable type: 'list'",)

感谢你们对我的帮助

data_lists = [{'x': '1', 'y': '2', 'z': 3}]
#first, iterate over the list items, which in this case there is only one
for i in data_lists:
    #then, select thee key you want
    print(i['y'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM