0

Given following dictionary:

recs = [
    {'id': 1,
     'custom': {
         {'tag': 'A'},
         {'tag': 'B'},
         {'tag': 'C'},
         {'name': 'Max'}

     }
     },

    {'id': 2,
     'custom': {
         {'tag': 'A'},
         {'tag': 'C'},
         {'note': 'Note for 2'}

     }
     },

    {'id': 3,
     'custom': {
         {'tag': 'B'},
         {'tag': 'C'},
         {'value': 12}
     }
     },

    {'id': 4,
     'custom': {
         {'tag': 'A'},
         {'tag': 'B'},
         {'tag': 'C'}
     }
     }
]

What would be the most optimal solution to search by list of tags ideally without additional modules, such as Pandas.

For example: tag == [A, B, C] will return

id=1 and id=4

Maksim
  • 16,635
  • 27
  • 94
  • 135

2 Answers2

0
taglist_dictionnary = {}
for item in recs :
  for key in item.keys() :
    if key == 'id':
      id = item[key]
      print(id)
    if key == 'custom':
      taglist = []
      tagdict = item[key]
      for tagkey in tagdict.keys() :
        if tagkey == 'tag':
          taglist.append(tagdict[tagkey])
      taglist_dictionnary.update({id : taglist})

print(taglist_dictionnary)

This will give you something like :

{'1':[A,B,C], '2',[A,C] ... }

And so on. It May be a more usefull architecture to check the existance of your tags in lists of each key then?

But actually, your dictionnary doesn't work, because it is unhashable. See this thread : TypeError: unhashable type: 'dict'

Osamoele
  • 371
  • 4
  • 15
0

Most optimal solution is probably quite a dangerous question to ask. The following snippet will probably get your result reasonably fast, but mind that it doesn't take duplicate values into account. If you are looking for 'most optimal' solutions, you probably want to be very sure of your context and fit your solution to that.

values_to_check = ['A', 'B', 'C']
num_values = len(values_to_check)

def check_dict(d, vtc):
    return [v for v in vtc if v in d['custom'].values()]

ids = [d['id'] for d in recs if len(check_dict(d, values_to_check)) == num_values]
Erik
  • 722
  • 4
  • 11