Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

python - How to match a dictionary's key value to another dictionaries value and append the results?

I have the following: list of dictionaries and a dictionary where the key is the value of key "userid" in my list of dictionaries. I want to take the data within dictionary extra and add it to each dictionary based on if the userid matches. Below is my sample data and what I have tried.

data = [{'date':'2021-01-01', 
         'userid': 'ABC123', 
         'name': 'John Smith', 
         'age': 15}, 
         {'date':'2021-01-10', 
         'userid': 'DEF123', 
         'name': 'Jane Doe', 
         'age': 19}]

extra = {'ABC123' : {"favorite sport": "basketball",
                     "favorite color": "red"},
         'DEF123': {"favorite sport": "soccer",
                     "favorite color": "yellow"}}  

def combine(data, extra): 
    data_new = data.copy()
    extra_new = extra.copy() 
    ids = list(extra_new.keys())

    output = []
    for value in data_new:
        value.update(extra_new)
        output.append(value)
    return output      

The above results in

output = [{'date':'2021-01-01', 
         'userid': 'ABC123', 
         'name': 'John Smith', 
         'age': 15,
         'ABC123' : {"favorite sport": "basketball",
                     "favorite color": "red"},
         'DEF123': {"favorite sport": "soccer",
                     "favorite color": "yellow"}} 
         {'date':'2021-01-10', 
         'userid': 'DEF123', 
         'name': 'Jane Doe', 
         'age': 19, 
         'ABC123' : {"favorite sport": "basketball",
                     "favorite color": "red"},
         'DEF123': {"favorite sport": "soccer",
                     "favorite color": "yellow"}}]

What I want is:

output = [{'date':'2021-01-01', 
         'userid': 'ABC123', 
         'name': 'John Smith', 
         'age': 15,
         "favorite sport": "basketball",
         "favorite color": "red"} 
         {'date':'2021-01-10', 
         'userid': 'DEF123', 
         'name': 'Jane Doe', 
         'age': 19, 
         "favorite sport": "soccer",
         "favorite color": "yellow"}]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems I have come up with a quite simple solution if I understood Your problem correctly.
The only thing I've edited is the combine function and it looks like this:

def combine(data, extra):
    data_new = data.copy()

    for i in range(len(data_new)):
        if data_new[i]['userid'] in extra:
            data_new[i].update(extra[data_new[i]['userid']])
    return data_new

The main two issues in Your code are:

  • You didn't check for dictionary key correspondence(although You stored the keys in a variable)
  • You didn't specify any dictionary of data, in which the extra values should be added

Also, not a big thing, but it seems unnecessary to copy the extra dictionary since you're not changing anything about it in this particular function.

P.S. I'm very new to answering, so I really hope this helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...