如何从 python 中的字典列表中获取值?

How to get values from list of dictionary in python?

我正在尝试从特定元素获取值并将它们附加为单个列表。

输入:

X = [
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Action": [
                    "iam:CreateInstanceProfile",
                    "iam:DeleteInstanceProfile",
                    "iam:GetRole",
                    "iam:GetInstanceProfile",
                    "iam:GetPolicy",
                    "iam:ListGroupPolicies",
                    "iam:RemoveRoleFromInstanceProfile",
                    "iam:CreateRole",
                    "iam:DeleteRole",
                    "iam:PutRolePolicy",
                    "iam:ListInstanceProfiles",
                    "iam:GetGroup",
                    "iam:AddRoleToInstanceProfile",
                    "iam:PassRole",
                    "iam:GetUserPolicy",
                    "iam:DeleteRolePolicy",
                    "iam:UpdateRole",
                    "iam:GetGroupPolicy",
                    "iam:GetUser",
                    "iam:ListRolePolicies",
                    "iam:GetRolePolicy",
                ],
                "Resource": [
                    "arn:aws:iam::*:policy/*",
                    "arn:aws:iam::*:instance-profile/*",
                    "arn:aws:iam::*:user/*",
                    "arn:aws:iam::*:role/*",
                    "arn:aws:iam::*:group/*",
                ],
                "Effect": "Allow",
                "Sid": "VisualEditor0",
            },
            {
                "Action": ["iam:ListPolicies", "iam:ListRoles", "iam:ListGroups"],
                "Resource": "*",
                "Effect": "Allow",
                "Sid": "VisualEditor1",
            },
        ],
    }
]

预期输出:

Y = [
    "iam:CreateInstanceProfile",
    "iam:DeleteInstanceProfile",
    "iam:GetRole",
    "iam:GetInstanceProfile",
    "iam:GetPolicy",
    "iam:ListGroupPolicies",
    "iam:RemoveRoleFromInstanceProfile",
    "iam:CreateRole",
    "iam:DeleteRole",
    "iam:PutRolePolicy",
    "iam:ListInstanceProfiles",
    "iam:GetGroup",
    "iam:AddRoleToInstanceProfile",
    "iam:PassRole",
    "iam:GetUserPolicy",
    "iam:DeleteRolePolicy",
    "iam:UpdateRole",
    "iam:GetGroupPolicy",
    "iam:GetUser",
    "iam:ListRolePolicies",
    "iam:GetRolePolicy",
    "iam:ListPolicies",
    "iam:ListRoles",
    "iam:ListGroups",
]

我在这里尝试获取所有 'Action' 的值并将它们存储为一个列表。 我该怎么做?

提前致谢。

尝试:

Y = [j for i in [i['Action'] for i in X[0]['Statement']] for j in i]

现在您可以通过以下方式打印:

print(Y)

注意变量命名时python建议不要使用小写


如果您不喜欢 Ultra-One-Liners,请尝试:

Y = []
for i in [i['Action'] for i in X[0]['Statement']]:
    Y.append(j for j in i)

如果您从不喜欢单行本:

Y = []
for i in X[0]['Statement']:
    for j in i['Action']:
        Y.append(j)

单行说明:

  • 首先它找出 i,这是你 X 中的所有 Action
  • 然后找到j,也就是i中的每个元素。

这里:

X = [
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Action": [
                    "iam:CreateInstanceProfile",
                    "iam:DeleteInstanceProfile",
                    "iam:GetRole",
                    "iam:GetInstanceProfile",
                    "iam:GetPolicy",
                    "iam:ListGroupPolicies",
                    "iam:RemoveRoleFromInstanceProfile",
                    "iam:CreateRole",
                    "iam:DeleteRole",
                    "iam:PutRolePolicy",
                    "iam:ListInstanceProfiles",
                    "iam:GetGroup",
                    "iam:AddRoleToInstanceProfile",
                    "iam:PassRole",
                    "iam:GetUserPolicy",
                    "iam:DeleteRolePolicy",
                    "iam:UpdateRole",
                    "iam:GetGroupPolicy",
                    "iam:GetUser",
                    "iam:ListRolePolicies",
                    "iam:GetRolePolicy",
                ],
                "Resource": [
                    "arn:aws:iam::*:policy/*",
                    "arn:aws:iam::*:instance-profile/*",
                    "arn:aws:iam::*:user/*",
                    "arn:aws:iam::*:role/*",
                    "arn:aws:iam::*:group/*",
                ],
                "Effect": "Allow",
                "Sid": "VisualEditor0",
            },
            {
                "Action": ["iam:ListPolicies", "iam:ListRoles", "iam:ListGroups"],
                "Resource": "*",
                "Effect": "Allow",
                "Sid": "VisualEditor1",
            },
        ],
    }
]

一个简单、整洁的生成器函数:

def get_all_actions(documents):
    for doc in documents:
        for stmt in doc.get("Statement", []):
            yield from stmt.get("Action", [])

使用您的输入:

>>> print(list(get_all_actions(X)))
['iam:CreateInstanceProfile', 'iam:DeleteInstanceProfile', 'iam:GetRole', 'iam:GetInstanceProfile', 'iam:GetPolicy', 'iam:ListGroupPolicies', 'iam:RemoveRoleFromInstanceProfile', 'iam:CreateRole', 'iam:DeleteRole', 'iam:PutRolePolicy', 'iam:ListInstanceProfiles', 'iam:GetGroup', 'iam:AddRoleToInstanceProfile', 'iam:PassRole', 'iam:GetUserPolicy', 'iam:DeleteRolePolicy', 'iam:UpdateRole', 'iam:GetGroupPolicy', 'iam:GetUser', 'iam:ListRolePolicies', 'iam:GetRolePolicy', 'iam:ListPolicies', 'iam:ListRoles', 'iam:ListGroups']
>>>
Y=X[0]['Statement'][0]['Action']