按键从 Python 列表中获取值。如何做到这一点?

Get values from Python list by key. How to achieve this?

我在 Python 列表中有以下值。

my_list = [{'Email': 'testemail@gmail.com', 'Fax': '125485795', 'Hash': '1', 'Comment': 'Foo', 'Product': 'bar'}]
print(type(my_list))

<class 'list'>

谁能帮我访问每个列表项?例如,如何获取 'Hash' 项目列表的值“1”?

my_list 只是一个里面只有一个字典的列表,所以你可以这样做:

my_list = [{'Email': 'testemail@gmail.com', 'Fax': '125485795', 'Hash': '1', 'Comment': 'Foo', 'Product': 'bar'}]

print(my_list[0]["Hash"]) # 1

像这样迭代字典的内容:

for key, item in my_list[0].items():
    print("key: " + key + " - item: " + item)

输出:

key: Comment - item: Foo

key: Fax - item: 125485795

key: Hash - item: 1

key: Email - item: testemail@gmail.com

key: Product - item: bar