我有一个坐在python列表内的字典。我如何遍历它?我需要同时获取键和值:
z = (client.list_signing_certificates(UserName=user_name)['Certificates'])
print(z)
[{'UserName': 'tdunphy', 'CertificateId': 'MSLQC5IFBTSNJOBEMIYXNN3DWC372237', 'CertificateBody': '-----BEGIN CERTIFICATE-----\nMIICWzCCAcSgAwIBAgIJAOrfAe4tcD4yMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV\nBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\naWRnaXRzIFB0eSBMdGQwHhcNMTkwNDI0MTMxMjUzWhcNMjkwNDIxMTMxMjUzWjBF\nMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\nZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\ngQDUMtMJ89aEflMt8HIB/wXxDVmCCy+M6asQC66oCIlXZA9Jrxc6ixLcI0Xhj0Fc\n/dU9VxAiVI9/ERlzO0FtSslWW9Qj3+Pp/XUyzeNDbI0VNIl8v4iQNbt5DLUbMBFI\nbbLheDJNt+xlSZK6cq5r7do0ChI9cZVQeAH9bvWkpPbiSQIDAQABo1MwUTAdBgNV\nHQ4EFgQUuLbgt2ftH2USaJ8Xku1ajdKL6r4wHwYDVR0jBBgwFoAUuLbgt2ftH2US\naJ8Xku1ajdKL6r4wDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQCN\nTCPXqPXyEQsWlUDT0A0Pjt3E5pELlUZ4jfUf/wU1OJDOZhv8jCr1nIcnYEECnjdS\noGifOBPeyJxKfUoSoFGNFHfgkaIRn0LaTGHCiyQQZNAnX9Jfqf7AAK8iSXphgthH\nUWQqyrpL83zcGAfIa3hyez3iEZ/P9zMEPSuhuwIbIA==\n-----END CERTIFICATE-----\n', 'Status': 'Active', 'UploadDate': datetime.datetime(2019, 4, 24, 13, 32, 31, tzinfo=tzutc())}, {'UserName': 'tdunphy', 'CertificateId': 'JPVTB5JCCAZ4COLGLG3QT2E2HBRKVZZP', 'CertificateBody': '-----BEGIN CERTIFICATE-----\nMIICWzCCAcSgAwIBAgIJAJmLouu5GeqxMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV\nBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\naWRnaXRzIFB0eSBMdGQwHhcNMTkwNDI0MTMzNDIyWhcNMjkwNDIxMTMzNDIyWjBF\nMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\nZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\ngQDUMtMJ89aEflMt8HIB/wXxDVmCCy+M6asQC66oCIlXZA9Jrxc6ixLcI0Xhj0Fc\n/dU9VxAiVI9/ERlzO0FtSslWW9Qj3+Pp/XUyzeNDbI0VNIl8v4iQNbt5DLUbMBFI\nbbLheDJNt+xlSZK6cq5r7do0ChI9cZVQeAH9bvWkpPbiSQIDAQABo1MwUTAdBgNV\nHQ4EFgQUuLbgt2ftH2USaJ8Xku1ajdKL6r4wHwYDVR0jBBgwFoAUuLbgt2ftH2US\naJ8Xku1ajdKL6r4wDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQAK\nI2kAzJDnqfAMcsqhTBJRF5zdE4oIw9EhjJLb0S462EVzZARIeTkqhn8nCAiigtJy\nkxZHGRCiVEFL0z1932vyB2e4crvIsLD7CYWQgxI7cLaGAyRcVIWs5i+qbrw7zOkU\nxmMtlfD3C3ju4bH9+bO2rymBynJldfARujrv0oVTcw==\n-----END CERTIFICATE-----\n', 'Status': 'Active', 'UploadDate': datetime.datetime(2019, 4, 24, 13, 35, 2, tzinfo=tzutc())}]
列表中有2个证书。我需要两者兼而有之。如果还有更多的证书,我也需要获得这些证书。视用户而定。
我正在使用python3。
答案 0 :(得分:1)
不知道到底是什么问题:
a={'a':1,'b':3}
b= {'c':1,'d':3}
l=[a,b]
for i in l :
print(i)
print(i.keys())
print(i.values())
答案 1 :(得分:1)
您可以遍历列表,然后遍历词典项:
for element in z:
for key, value in element.items():
print("Key is: ", key)
print("Value is: ", value)
dict.items()
返回一个包含每个键和关联值的元组。
否则,如果只需要键,则可以使用dict.keys()
,或者,如果只需要值,则可以使用dict.values()
。
编辑:
如果您要获取证书正文,那么您甚至不需要迭代字典,可以这样做:
certificates = []
for element in z:
certificates.append(element["CertificateBody"])
或作为一种理解:
certificates = [element["CertificateBody"] for element in z]
答案 2 :(得分:0)
您只需要获取字典,然后遍历字典项即可。
for key, value in z[0].items():
print(key, value)
答案 3 :(得分:-1)
您需要先遍历列表。然后,对列表中的每本字典进行遍历。
for i, d in z:
for y, value in d:
print(key)
print(value)