如何从python中的字典列表中获取值?
Posted
技术标签:
【中文标题】如何从python中的字典列表中获取值?【英文标题】:How to get values from list of dictionary in python? 【发布时间】:2020-04-09 17:59:40 【问题描述】:我正在尝试从特定元素中获取值并将它们附加为单个列表。
输入:
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",
]
在这里,我试图获取所有“操作”的值并将它们存储为单个列表。 我该怎么做?
提前致谢。
【问题讨论】:
请重复介绍,尤其是how to ask。许多在线教程和其他参考资料向您展示了如何访问列表和字典的元素。您只需将两者结合起来。给您带来麻烦的代码在哪里? 你尝试了什么? 我的回答对你有帮助吗? @Joshua Varghese。它有很大帮助。谢谢 【参考方案1】:试试:
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)
如果你从不喜欢 One-Liners:
Y = []
for i in X[0]['Statement']:
for j in i['Action']:
Y.append(j)
单线解释:
首先它找出i
,这是你X
中的所有Action
s。
然后找到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",
,
],
]
【讨论】:
更加完美。【参考方案2】:一个简单、整洁的生成器函数:
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']
>>>
【讨论】:
【参考方案3】:Y=X[0]['Statement'][0]['Action']
【讨论】:
在编辑 Y 列表时要小心 - 您也会编辑 X。您没有直接的 dict,它是列表 X 的第一个元素。以上是关于如何从python中的字典列表中获取值?的主要内容,如果未能解决你的问题,请参考以下文章
如何从字典列表中获取值 - Python 3.7.1 [重复]