简体   繁体   中英

Enumerate through list having dictionary in it using python

I have a structure that looks like this:

{ 
"permutations": [
     {
        "testname": "Test1",
        "file_type": "file1",
        "test_options": {
            "rule_entities": [
                "name1"
         ],
        },
        ],
        "userType": "user",
        "rulename": "rule1",
        "action": "alert",
      },
     {
        "testname": "Test2",
        "file_type": "file2",
        "test_options": {
            "rule_entities": [
                "name2"
            ],
        },
        ],
        "userType": "user",
        "rulename": "rule2",
        "action": "alert",
      }
}

I want to make the changes in the above structure testname and rulename by appending both the variables with rule_entities values such that testname: "Test1_name1" and rulename: "rule1_name1" where name1 is from rule_entities array. Hence, the above structure should be the final structure

{ 
"permutations": [
     {
        "testname": "Test1_name1",
        "file_type": "file1",
        "test_options": {
            "rule_entities": [
                "name1"
         ],
        },
        ],
        "userType": "user",
        "rulename": "rule1_name1",
        "action": "alert",
      },
     {
        "testname": "Test2_name2",
        "file_type": "file2",
        "test_options": {
            "rule_entities": [
                "name2"
            ],
        },
        ],
        "userType": "user",
        "rulename": "rule2_name2",
        "action": "alert",
      }
}

I gave it a try using the following code snipet

for index, d in enumerate['permutations']:
    testname = d['permutations'][index]['testname']
    testname_new = testname + '_' + rule_entitities


for index, d in enumerate['permutations']:
    rulename = d['permutations'][index]['rulename']
    rulename_new = rulename + '_' + rule_entities

I am not getting the desired output. Thanks in advance!!

You never updated the value stored in the dictionary

for index, d in enumerate['permutations']:
    testname = d['permutations'][index]['testname']
    testname_new = testname + '_' + rule_entities
    d['permutations'][index]['testname'] = testname_new


for index, d in enumerate['permutations']:
    rulename = d['permutations'][index]['rulename']
    rulename_new = rulename + '_' + rule_entities
    d['permutations'][index]['rulename'] = rulename_new

I got it working using the below code:

for x in 'permutations':
    rule_entity_slash = ['permutations'][0]['test_options']['rule_entities'][0]
    rule_entity_underscore = rule_entity_slash.replace("/","_")
    x['testname'] = x['testname'] + '_' + rule_entity_underscore
    x['rulename'] = x['rulename'] + '_' + rule_entity_underscore

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM