I have an object that is a list of lists of dictionaries:
myObject =[[{ "play": 5.00, "id": 1, "uid": "abc" }, \
{ "play": 1.00, "id": 2, "uid": "def" }], \
[{ "play": 6.00, "id": 3, "uid": "ghi" }, \
{ "play": 7.00, "id": 4, "uid": "jkl" }], \
[{ "play": 3.00, "id": 5, "uid": "mno" }, \
{ "play": 1.00, "id": 6, "uid": "pqr" }]]
I want to sort the list by the sum of play values in the dictionaries of each nested list. The object would then be sorted like this:
myObject =[[{ "play": 6.00, "id": 3, "uid": "ghi" }, \
{ "play": 7.00, "id": 4, "uid": "jkl" }], \
[{ "play": 5.00, "id": 1, "uid": "abc" }, \
{ "play": 1.00, "id": 2, "uid": "def" }], \
[{ "play": 3.00, "id": 5, "uid": "mno" }, \
{ "play": 1.00, "id": 6, "uid": "pqr" }]]
If it were just a list of dicts then:
sorted(myObject, key=sum(map(itemgetter(play))), reverse=True)
would work. I can't figure out how to do this without looping over the list, calculating the sum, then sorting. That is what I am doing now, but I'm trying to increase the efficiency of this code by removing loops because my list has 100's of millions of lists in it.
\line continuations in this case. Since the lines end with a comma and the list literals are still “open”, Python will automatically expect the next line to continue there.