簡體   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