简体   繁体   中英

How can I join the rest of the field to Array after processing the group using more than one key?

I want to load data to "dict_array" and make a group based on 'a' and 'b' keys and put the rest of the keys and values in the array. Forced to compare key and value with for loop and process it, but it's too slow. Is there a way to take care of it quickly?

  • original
dict_array=[
    {'a':1,'b':1,'c':11,'d':21},
    {'a':1,'b':1,'c':12,'d':22},
    {'a':1,'b':1,'c':13,'d':23}
]
  • grouped
dict={
    'a':1,
    'b':1,
    'array':[
        {'c':11,'d':21},
        {'c':12,'d':22},
        {'c':13,'d':23}    
    ]
}
  • sample function
def dict_search_in_array(dict_array,search_dict):
    idx=0
    for dict in dict_array:
        if dict['a'] == search_dict['a'] and dict['b'] == search_dict['b'] :
            return True,idx
        idx=idx+1
    return False,-1

def dict_bucket(dict_array,add_dict):
    is_bucketed,idx = dict_search_in_array(dict_array,add_dict)
    if is_bucketed :
        # TODO : append array
        print('Bucketed :',idx)
    else:
        # TODO : add dict
        print('Not Bucketed')

I don't know if it will be any faster, but you can use sets to find the intersections and differences between the lists:

dict_array=[
    {'a':1,'b':1,'c':11,'d':21},
    {'a':1,'b':1,'c':12,'d':22},
    {'a':1,'b':1,'c':13,'d':23}
]

# Convert each dict in dict_array into a set
sets_array = [set(tuple(x.items())) for x in dict_array]

# Find the intersection of all sets
intersect = set.intersection(*sets_array)
# {('b', 1), ('a', 1)}

# Find the differences between each set in the array and the intersection
diff_array = [set.difference(x, intersect) for x in sets_array]

# Create a new dict, adding the difference sets as dicts to an array
new_dict = {'array': [dict(x) for x in diff_array]}

# Add on the intersection set as a dict
new_dict.update(intersect)


print(new_dict)
# {'array': [{'d': 21, 'c': 11}, {'c': 12, 'd': 22}, {'c': 13, 'd': 23}], 'b': 1, 'a': 1}

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