从字典中获取第一个元素,它是一个数组-python

从字典中获取第一个元素,它是一个数组-python,python,http-headers,Python,Http Headers,我有一个数组存储头信息: {'x-frame-options': {'defined': True, 'warn': 0, 'contents': 'SAMEORIGIN'}, 'strict-transport-security': {'defined': True, 'warn': 0, 'contents': 'max-age=15552000'}, 'access-control-allow-origin': {'defined': False, 'warn': 1, 'contents

我有一个数组存储头信息:

{'x-frame-options': {'defined': True, 'warn': 0, 'contents': 'SAMEORIGIN'}, 'strict-transport-security': {'defined': True, 'warn': 0, 'contents': 'max-age=15552000'}, 'access-control-allow-origin': {'defined': False, 'warn': 1, 'contents': ''}, 'content-security-policy': {'defined': True, 'warn': 0, 'contents': "upgrade-insecure-requests; frame-ancestors 'self' https://stackexchange.com"}, 'x-xss-protection': {'defined': False, 'warn': 1, 'contents': ''}, 'x-content-type-options': {'defined': False, 'warn': 1, 'contents': ''}}
我想得到字典的第一个元素

#header is a return array that store all header information,

headers = headersecurity.verify_header_existance(url, 0)
for header in headers:
    if header.find("x-frame-options"):
        for headerSett in header:
            defined = [elem[0] for elem in headerSett.values()] # here I don't get first element
            print(defined)
预期结果是:

x-frame-options : defined = True;
access-control-allow-origin : defined = True;
x-content-type-options : defined = True;
....

谢谢

我认为这样使用字典键会更安全

headers['x-frame-options']['defined']
这样,您就不依赖于dict中的排序,dict是不排序的

编辑:刚刚看到您的编辑和您期望的输出,这里有一个简单的方法:

for key, value in headers.items():
    if "defined" in value:
        print(f"{key} : defined = {value['defined']}")
输出:

x-frame-options : defined = True
strict-transport-security : defined = True
access-control-allow-origin : defined = False
content-security-policy : defined = True
x-xss-protection : defined = False
x-content-type-options : defined = False

头文件中根本没有数组,因此无法使用第一个元素。请解释一下你所期望的无序的价值。没有第一个。为什么你不能用它的键来索引它呢?我想得到一个值:defined…所以用defined作为你的键。elem[defined],而不是elem[0]。但我希望以一种为所有其他标题定义的方式来定义它一个简单的问题,如何仅打印每个标题定义的值?只需替换print语句中的内容即可