Please help me to find the same value in the dictionary in the Python two lists and make one list

Asked 1 years ago, Updated 1 years ago, 121 views

 ex_list_1 = [{"name":"kim", "age":35, "height":185}, {"name":"Lee", "age":15, "height":165}]
 ex_list_2 = [{"first_name":"kim", "nickname":"kkk"}]

When there are two lists of this type, I would like to combine the dict() of ex_list_1 and ex_list_2 with the same value (but some of them have the same value as Kim as above, and the key names of the same value The final appearance I want is as follows

ex_final = [{"name":"kim", "age":35, "height":185, "nickname":kkk"},{"name":Lee", "age":15, "height":165, "nickname":None}]

I want to combine it with ex_final, is there a way? I can't think of it because I have to find and put the same value in the list. Masters, please help me (For your information, the None value is just an example, and I think it's okay to enter a blank string or something.)

python dictionary python-2.7 list

2022-09-22 18:26

2 Answers

import pandas as pd

ex_list_1 = [{"name":"kim", "age":35, "height":185},
             {"name":"Lee", "age":15, "height":165}]
ex_list_2 = [{"first_name":"kim", "nickname":"kkk"}]

df1 = pd.DataFrame(ex_list_1)
df2 = pd.DataFrame(ex_list_2)

df3 = df1.merge(df2, left_on='name', right_on='first_name', how='outer')

res = df3[['age', 'height', 'name', 'nickname']].to_dict(orient='records')

[{'age': 35, 'height': 185, 'name': 'kim', 'nickname': 'kkk'},
 {'age': 15, 'height': 165, 'name': 'Lee', 'nickname': nan}]

It would be convenient to change it to a Pandas data frame.


2022-09-22 18:26

Pandas for data processing is certainly an easy-to-handle and great module, but it's a third-party library, so you need to learn how to install and use it, and you need to take a toll on performance.

If it's simple data handling, processing within the underlying library can be a good way to maintain concise code.

In [1]: ex_list_1 = [{"name":"kim", "age":35, "height":185},{"name":"Lee", "age" :15, "height":165}]                                                     

In [2]: ex_list_2 = [{"first_name":"kim", "nickname":"kkk"}]

In [3]: def mergeMembership(D1, D2): 
    ...:     ...:     temp = {**D1} 
    ...:     ...:     for k1, v1 in D1.items(): 
    ...:         ...:         for k2, v2 in D2.items(): 
    ...:             if v1 == v2: # whether the value is the same 
    ...:                 ...:                 temp.update(D2) 
    ...:                 temp.pop(k2) #Remove duplicate elements (first_name) 
    ...:                 ...:                 return temp 
    ...:             ...:             else: 
    ...:                 if k2!= 'first_name': temp[k2] = None # The condition for first_name is not clear 
    ...:         ...:         return temp                                                                                                                

In [4]: mergeMembership(ex_list_1[0], ex_list_2[0])                                                                            
Out[4]: {'name': 'kim', 'age': 35, 'height': 185, 'nickname': 'kkk'}

In [5]: mergeMembership(ex_list_1[1], ex_list_2[0])                                                                            
Out[5]: {'name': 'Lee', 'age': 15, 'height': 165, 'nickname': None}


2022-09-22 18:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.