Get all first items of any unknown content in dict

Viewed 69

I have nested dictionary like this one:

results={
    0: {'id': 87535653, 'cc': 0, 'cover': 89, 'grid': 'VQ'},
    1: {'id': 31213450, 'cc': 0, 'cover': 99, 'grid': 'VQ'},
    2: {'id': 22343446, 'cc': 0.1, 'cover': 79, 'grid': 'VP'},
    3: {'id': 34568756, 'cc': 0, 'cover': 34, 'grid': 'VQ'},
    4: {'id': 43532251, 'cc': 0.2, 'cover': 78, 'grid': 'DS'},
    5: {'id': 42532376, 'cc': 23, 'cover': 90, 'grid': 'ZF'},
}

What I want is to get only the first item of any content in grid. So, for this example I want to get back this:

results={
    0: {'id': 87535653, 'cc': 0, 'cover': 89, 'grid': 'VQ'},
    2: {'id': 22343446, 'cc': 0.1, 'cover': 79, 'grid': 'VP'},
    4: {'id': 43532251, 'cc': 0.2, 'cover': 78, 'grid': 'DS'},
    5: {'id': 42532376, 'cc': 23, 'cover': 90, 'grid': 'ZF'},
}

The challenge: I don't know the content in Grid before. That is, anything can be in there and I can't iterate according to the specific content. The code has to recognize independently that this is an item that does not yet exist with this content.

How can I iterate over the files to get the result I want?

3 Answers

You can iterate over the dict and mark each grid you found, that way the next time you find it you dont add it to the final dict

In [1]: results={
   ...:     0: {'id': 87535653, 'cc': 0, 'cover': 89, 'grid': 'VQ'},
   ...:     1: {'id': 31213450, 'cc': 0, 'cover': 99, 'grid': 'VQ'},
   ...:     2: {'id': 22343446, 'cc': 0.1, 'cover': 79, 'grid': 'VP'},
   ...:     3: {'id': 34568756, 'cc': 0, 'cover': 34, 'grid': 'VQ'},
   ...:     4: {'id': 43532251, 'cc': 0.2, 'cover': 78, 'grid': 'DS'},
   ...:     5: {'id': 42532376, 'cc': 23, 'cover': 90, 'grid': 'ZF'},
   ...: }

In [2]: final_dict = {}

In [3]: _collected_grids = set()

In [4]: for key, value in results.items():
   ...:     if value['grid'] not in _collected_grids:
   ...:         final_dict[key] = value
   ...:         _collected_grids.add(value['grid'])
   ...:

In [5]: final_dict
Out[5]:
{0: {'id': 87535653, 'cc': 0, 'cover': 89, 'grid': 'VQ'},
 2: {'id': 22343446, 'cc': 0.1, 'cover': 79, 'grid': 'VP'},
 4: {'id': 43532251, 'cc': 0.2, 'cover': 78, 'grid': 'DS'},
 5: {'id': 42532376, 'cc': 23, 'cover': 90, 'grid': 'ZF'}}

See below (the idea is to keep a "note" of the grid data you already scanned)

results = {
    0: {'id': 87535653, 'cc': 0, 'cover': 89, 'grid': 'VQ'},
    1: {'id': 31213450, 'cc': 0, 'cover': 99, 'grid': 'VQ'},
    2: {'id': 22343446, 'cc': 0.1, 'cover': 79, 'grid': 'VP'},
    3: {'id': 34568756, 'cc': 0, 'cover': 34, 'grid': 'VQ'},
    4: {'id': 43532251, 'cc': 0.2, 'cover': 78, 'grid': 'DS'},
    5: {'id': 42532376, 'cc': 23, 'cover': 90, 'grid': 'ZF'},
}

grids = set()
data = dict()
for k, v in results.items():
    if v['grid'] not in grids:
        data[k] = v
        grids.add(v['grid'])
for k, v in data.items():
    print(f'{k} {v}')

output

0 {'id': 87535653, 'cc': 0, 'cover': 89, 'grid': 'VQ'}
2 {'id': 22343446, 'cc': 0.1, 'cover': 79, 'grid': 'VP'}
4 {'id': 43532251, 'cc': 0.2, 'cover': 78, 'grid': 'DS'}
5 {'id': 42532376, 'cc': 23, 'cover': 90, 'grid': 'ZF'}

You can do it with Pandas:

import pandas as pd    
df=pd.DataFrame.from_dict(results, orient='index')
df=df.drop_duplicates('grid')
res = df.to_dict(orient='index')

>>>print(res)
{0: {'id': 87535653, 'cc': 0.0, 'cover': 89, 'grid': 'VQ'}, 
2: {'id': 22343446, 'cc': 0.1, 'cover': 79, 'grid': 'VP'}, 
4: {'id': 43532251, 'cc': 0.2, 'cover': 78, 'grid': 'DS'}, 
5: {'id': 42532376, 'cc': 23.0, 'cover': 90, 'grid': 'ZF'}}
Related