Home > Blockchain >  Python -- Remove dictionary from multiple nested list of dictionary
Python -- Remove dictionary from multiple nested list of dictionary

Time:05-06

I want to remove all of dict element in 'qas' list when its f1 is not 1.0.

Here is the list:

test_list = [
    {'paragraphs': [{
        'qas':[
            {'q':'abc','id':'123','a':['4','d'],'f1':1.0},
            {'q':'dsf','id':'343','a':['6','d'],'f1':0.5},
            {'q':'wre','id':'565','a':['4','u'],'f1':0.2}
        ]}
    ]},
    
    {'paragraphs': [{
        'qas':[
            {'q':'ujn','id':'874','a':['4','d'],'f1':1.0},
            {'q':'yht','id':'454','a':['5','d'],'f1':0.7},
            {'q':'nth','id':'676','a':['4','j'],'f1':0.4}
        ]}
    ]},
] 

My expect result is:

test_list = [
    
    {'paragraphs': [{
        'qas':[
            {'q':'abc','id':'123','a':['4','d'],'f1':1.0}
        ]}
    ]},
    
    {'paragraphs': [{
        'qas':[
            {'q':'ujn','id':'874','a':['4','d'],'f1':1.0}
        ]}
    ]},
    
]

My current code:

for i in range(len(test_list)): 
    for j in range(len(test_list[i]['paragraphs'][0]['qas'])):
        if test_list[i]['paragraphs'][0]['qas'][j]['f1'] != 1:
            del test_list[i]['paragraphs'][0]['qas'][j]

However I got an index error:

      1 for i in range(len(test_list)):
      2     for j in range(len(test_list[i]['paragraphs'][0]['qas'])):
----> 3         if test_list[i]['paragraphs'][0]['qas'][j]['f1'] != 1:
      4             del test_list[i]['paragraphs'][0]['qas'][j]

IndexError: list index out of range

There are multiple layers of nested list and dict. Not sure how to solve this problem.

CodePudding user response:

IIUC, this can do the job.

for i in range(len(test_list)):
  test_list[i]["paragraphs"][0]["qas"] = test_list[i]["paragraphs"][0]["qas"][:1]

print(test_list)

Output -

[{'paragraphs': [{'qas': [{'a': ['4', 'd'],
      'f1': 1.0,
      'id': '123',
      'q': 'abc'}]}]},
 {'paragraphs': [{'qas': [{'a': ['4', 'd'],
      'f1': 1.0,
      'id': '874',
      'q': 'ujn'}]}]}]

CodePudding user response:

Tried this and it worked, but wanted to learn easier way to get this done.

import itertools

tmp = []
for i in range(len(test_list)): 
    for j in range(len(test_list[i]['paragraphs'][0]['qas'])):
        if test_list[i]['paragraphs'][0]['qas'][j]['f1'] != 1:
            tmp.append(False)
        else:
            tmp.append(True)        
    test_list[i]['paragraphs'][0]['qas'] = list(itertools.compress(test_list[i]['paragraphs'][0]['qas'],tmp))

CodePudding user response:

Code like this in Python will results in an index error,because del can change the length of the lst

lst = [1,2,3,4]
for i in range(len(lst)):
    if lst[i] == 1:
        del lst[i]

If you want to filter the elements in the lst, you just need to do this:

lst = [i for i in lst if i==1]
  • Related