Home > Mobile >  An efficient way to search elements in a Json array (dictionary of arrays)
An efficient way to search elements in a Json array (dictionary of arrays)

Time:12-04

I am writing a script that reads two Json files into dictionaries

The dictionaries are more or less similar, like this

{  "elements":[
    {
     "element_id":0,
     "thedata":{
                "this": 5
               }
     },
     {
     "element_id":4,
     "thedata":{
                "this": 5
               }
     }
    {
      ...
    }
]}

So far I had assumed that the element_id went from 0 and increased 1 by 1 Then the requirements changed and this time they went from 0 and increased 4 by 4 or something like this

Anyway, I though so far that both dictionaries would have the same number of elements and the same increasing distance so when I got the elements in my script I wrote something like

def process_elements(number):
    el1_id=thedictionary['elements'][number]['element_id']
    el2_id=thedictionary2['elements'][number]['element_id']
    assert(el1_id==el2_id)
    #here work with the data 

However the requirements have changed again

Now the number of elements of one dictionary are not necessarily the same as the other Also it is not guaranteed that one of them start always at 0

So now I have to find the elements in both dictionaries with the same element id

So my question is , in a dictionary like above (that came from a json) is there a quick way to find the element that has a particular element_id and get the element?

Something like

def process_elements(number):
    el1_id=thedictionary['elements'][number]['element_id']

    n=find_i(thedictionary2,el1_id) #finds the index with the element that has id the same as el1_id
    el2_id=thedictionary2['elements'][n]['element_id']
    assert(el1_id==el2_id)  #Of course they are the same since we used find_i
    #here work with the data 

It has to be quick since I use it for an animation

CodePudding user response:

If you need to find multiple elements with a particular element_id in a dictionary, and you want to do it as efficiently as possible, you could use a dictionary to store the elements with a given element_id. Then, when you need to find an element with a particular element_id, you can just look it up in the dictionary using the element_id as the key, without having to iterate over the elements in the dictionary.

Here's an example of how you could do this:

# Create a dictionary to store the elements with a given element_id
elements_by_id = {}

# Iterate over the elements in the dictionary
for element in thedictionary['elements']:
    # Get the element_id for the current element
    element_id = element['element_id']

    # Check if the element_id is already a key in the elements_by_id dictionary
    if element_id not in elements_by_id:
        # If the element_id is not already a key in the dictionary, create a new key-value pair in the dictionary,
        # with the element_id as the key and an empty list as the value
        elements_by_id[element_id] = []

    # Add the current element to the list of elements with the given element_id
    elements_by_id[element_id].append(element)

# Now, when you need to find the elements with a particular element_id, you can just look it up in the dictionary
# using the element_id as the key
found_elements = elements_by_id[4]

# Print the found elements to the console
print(found_elements)

This method is more efficient than iterating over the elements in the dictionary and checking each element's element_id value, because it only requires a single pass over the elements in the dictionary to create the elements_by_id dictionary, and then you can look up elements with a particular element_id in constant time.

If you want to make the code even faster, you could use the dict.setdefault() method to create the elements_by_id dictionary in a single pass over the elements in the dictionary. This method allows you to specify a default value to use if the key you're looking for doesn't already exist in the dictionary, so you don't have to check if the key exists before adding it to the dictionary.

Here's an example of how you could use the dict.setdefault() method to create the elements_by_id dictionary:

# Create a dictionary to store the elements with a given element_id
elements_by_id = {}

# Iterate over the elements in the dictionary
for element in thedictionary['elements']:
    # Get the element_id for the current element
    element_id = element['element_id']

    # Use the setdefault() method to create a new key-value pair in the dictionary,
    # with the element_id as the key and an empty list as the value, if the element_id is not already a key in the dictionary
    elements_by_id.setdefault(element_id, [])

    # Add the current element to the list of elements with the given element_id
    elements_by_id[element_id].append(element)

# Now, when you need to find the elements with a particular element_id, you can just look it up in the dictionary
# using the element_id as the key
found_elements = elements_by_id[4]

# Print the found elements to the console
print(found_elements)

This method is faster than the previous method because it only requires a single pass over the elements in the dictionary, and it doesn't require you to check if the element_id is already a key in the dictionary before adding it.

CodePudding user response:

Using the get() method:

# Create a dictionary to store the elements with a given element_id
elements_by_id = {}

# Iterate over the elements in the dictionary
for element in thedictionary['elements']:
    # Get the element_id for the current element
    element_id = element['element_id']

    # Check if the element_id is already a key in the elements_by_id dictionary
    if element_id not in elements_by_id:
        # If the element_id is not already a key in the dictionary, create a new key-value pair in the dictionary,
        # with the element_id as the key and an empty list as the value
        elements_by_id[element_id] = []

    # Add the current element to the list of elements with the given element_id
    elements_by_id[element_id].append(element)

# Now, when you need to find the elements with a particular element_id, you can use the dict.get() method
# to get the list of elements with the given element_id, and specify a default value to return if the element_id
# doesn't exist as a key in the dictionary
found_elements = elements_by_id.get(34554, [])

# Print the found elements to the console
print(found_elements)
  • Related