簡體   English   中英

如何從列表中提取值並將其存儲為字典(鍵值對)?

[英]How to extract values from list and store it as dictionary(key-value pair)?

我需要從這個字典列表中提取 2 個值並將其存儲為鍵值對。 在這里,我附加了示例數據。我需要從該輸入中提取“名稱”和“服務”並將其存儲為字典。 其中“名稱”是鍵,對應的“服務”是它的值。

輸入:

response  = {
'Roles': [
    {
        'Path': '/', 
        'Name': 'Heera', 
        'Age': '25', 
        'Policy': 'Policy1', 
        'Start_Month': 'January', 
        'PolicyDocument': 
            {
                'Date': '2012-10-17', 
                'Statement': [
                    {
                        'id': '', 
                        'RoleStatus': 'New_Joinee', 
                        'RoleType': {
                                'Service': 'Service1'
                                 }, 
                        'Action': ''
                    }
                ]
            }, 
        'Duration': 3600
    }, 
    {
        'Path': '/', 
        'Name': 'Prem', 
        'Age': '40', 
        'Policy': 'Policy2', 
        'Start_Month': 'April', 
        'PolicyDocument': 
            {
                'Date': '2018-11-27', 
                'Statement': [
                    {
                        'id': '', 
                        'RoleStatus': 'Senior', 
                        'RoleType': {
                                'Service': ''
                                 }, 
                        'Action': ''
                    }
                ]
            }, 
        'Duration': 2600
    }, 
    
    ]
}

從這個輸入中,我需要 output 作為字典類型。

Output 格式: {名稱:服務}

Output:

{ "Heera":"Service1","Prem" : " "}

我的嘗試:

Role_name =[]
response = {#INPUT WHICH I SPECIFIED ABOVE#}
roles = response['Roles']
for role in roles:
    Role_name.append(role['Name'])
print(Role_name)

我需要將名稱與其相應的服務配對。 任何幫助都會非常顯着。

提前致謝。

你只需要這樣做:

liste = []
for role in response['Roles']:
    liste.append(
            {
                role['Name']:role['PolicyDocument']['Statement'][0]['RoleType']['Service'],
            }
            )
print(liste)

看來您的輸入數據的結構有點奇怪,我不確定)在接下來的幾個月里在做什么,因為它們使事情變得無效,但這是一個工作腳本,假設您從輸入中刪除了括號。

response = {
    'Roles': [
        {
            'Path': '/',
            'Name': 'Heera',
            'Age': '25',
            'Policy': 'Policy1',
            'Start_Month': 'January',
'PolicyDocument':
{
    'Date': '2012-10-17',
    'Statement': [
        {
            'id': '',
            'RoleStatus': 'New_Joinee',
            'RoleType': {
                'Service': 'Service1'
            },
            'Action': ''
        }
    ]
},
'Duration': 3600
},
{
    'Path': '/',
    'Name': 'Prem',
    'Age': '40',
    'Policy': 'Policy2',
    'Start_Month': 'April',
'PolicyDocument':
{
    'Date': '2018-11-27',
    'Statement': [
        {
            'id': '',
            'RoleStatus': 'Senior',
            'RoleType': {
                'Service': ''
            },
            'Action': ''
        }
    ]
},
'Duration': 2600
},

]
}

output = {}

for i in response['Roles']:
    output[i['Name']] = i['PolicyDocument']['Statement'][0]['RoleType']['Service']

print(output)

你只需要寫一條長線,直到關鍵的“服務”。 你在Start_Month': 'January')'Start_Month': 'April')行中出現語法錯誤。 你不能有一個未閉合的括號。 修復它並運行以下命令。

這是代碼:

output_dict = {}
for r in response['Roles']:
    output_dict[r["Name"]] = r['PolicyDocument']['Statement'][0]['RoleType']['Service']

print(output_dict)

Output:

{'Heera': 'Service1', 'Prem': ''}

這應該在名為 role_services 的變量中為您提供所需的內容:

role_services = {}

for role in response['Roles']:
    for st in role['PolicyDocument']['Statement']:
        role_services[role['Name']] = st['RoleType']['Service']

它將確保您將 go 通過該數據結構中的所有語句,但請注意,您將在遍歷響應時覆蓋鍵值對,如果它們存在於多個條目中!

可能有幫助的 for 循環的參考,說明了在其中使用if語句,這可以幫助您擴展它以檢查項目是否已經存在!

希望有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM