Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have three lists in python that I retrieved from database.

[(34, 75), (7, 50), (36, 100)]
[(7, 73.3), (34, 100), (36, 65)]
[(34, 80), (36, 85.6),(7, 85)]
In following lists we have common first element of tuple like we have first element '7' in all three lists and same goes with '34' and '36' as well.First element is "id" and second element are "rating" in first list, "marks" in second list and "slots" in third list. I want to create a new list in which will put a unique entry by first element of tuple and then average of 2nd value from all three lists. I want my out put to look like

[(34, 85), (7, 69.43), (36, 83.53)]
first element of every tuple is common element from all three lists and second element is average of corresponding element from all three lists.
how to do it in python?

What I have tried:

I don't have idea how to do it thats why need code for it.
Posted
Updated 29-Nov-20 2:06am
Comments
Richard MacCutchan 29-Nov-20 5:48am    
Sorry, this site does not provide code to order. You are expected to write the code yourself.

1 solution

Here is a suggestion:
Create a dictionary
For each list:
Create a dictionary item using the id as the key and add the number to the value. Remember that the first time you need to set it rather than add it.
Keep a count of the number of values that are being added.

Loop through the dictionary entries calculating the averages based on the total value divided by the count.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900