Question:
I have a list in the following format:
x = [["hello",0,5], ["hi",0,6], ["hello",0,8], ["hello",1,1]]
The algorithm:
- Combine all inner lists with the same starting 2 values, the third value doesn't have to be the same to combine them
- e.g.
"hello",0,5
is combined with"hello",0,8
- But not combined with
"hello",1,1
- e.g.
- The 3rd value becomes the average of the third values:
sum(all 3rd vals) / len(all 3rd vals)
- Note: by
all 3rd vals
I am referring to the 3rd value of each inner list of duplicates - e.g.
"hello",0,5
and"hello",0,8
becomeshello,0,6.5
- Note: by
Desired output: (Order of list doesn't matter)
x = [["hello",0,6.5], ["hi",0,6], ["hello",1,1]]
Question:
- How can I implement this algorithm in Python?
Ideally it would be efficient as this will be used on very large lists.
If anything is unclear let me know and I will explain.
Edit: I have tried to change the list to a set to remove duplicates, however this doesn't account for the third variable in the inner lists and therefore doesn't work.
Solution Performance:
Thanks to everyone who has provided a solution to this problem! Here are the results based on a speed test of all the functions: