Summing up elements from one list based on indices in another list

user3641829

So here is what I am trying to achieve in Python:

  • I have a list "A" with unsorted and repeated indices.
  • I have a list "B" with some float values
  • Length A = Length B
  • I want list "C" with summed values of B based on the repeated indices in A in a sorted ascending manner.

Example:

A = [0, 1, 0, 3, 2, 1, 2]  # (indicates unsorted and repeated indices)
B = [25, 10, 15, 10, 5, 30, 50]  # (values to be summed)
C = [25+15, 10+30, 5+50, 15]  # (summed values in a sorted manner)

So far I know how to do the sorting bit with:

C = zip(*sorted(zip(A, B)))

Getting the result:

[(0, 0, 1, 1, 2, 2, 3), (15, 25, 10, 30, 5, 50, 10)] 

But I do not know how to do the sum.

What would be a good way to create list C?

Jan

Use zip() in combination with a dict:

A = [0 , 1 , 0 , 3 , 2 , 1 , 2] 
B = [25 , 10 , 15 , 10 , 5 , 30 , 50]

sums = {}
for key, value in zip(A,B):
    try:
        sums[key] += value
    except KeyError:
        sums[key] = value
print(sums)
# {0: 40, 1: 40, 2: 55, 3: 10}

And see a demo on ideone.com.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Haskell: summing up the elements of a list

From Dev

Haskell: summing up the elements of a list

From Dev

Remove certain elements in one list based on condition from another list

From Dev

Python: One liner for summing up elements in a list meeting a condition

From Dev

Repeat elements in one list based on elements from another

From Dev

Repeat elements in one list based on elements from another

From Dev

Filter Elements from a list based on another list

From Dev

Remove elements from a list, using another list as indices

From Dev

Check for elements in one list from another list

From Dev

Summing up every alternate 4 elements in list

From Dev

How to modify the elements of one list based on another

From Dev

match element of a list based on another list in python where elements of one list is the substring of elements of another list

From Dev

Remove parts of a list based on another list of indices

From Dev

Finding the indices of the values of one list in another list

From Java

Finding elements from a list included in another one

From Dev

C: moving elements from one list to another

From Dev

Maximum of element from List based on the another one

From Dev

Maximum of element from List based on the another one

From Dev

Summing elements of a list in r

From Dev

Summing List Elements

From Dev

How to remove elements from a list with lambda based on another list

From Dev

Python: Deleting a list element based on elements from another list

From Dev

Sum of specific elements of a list based on indexes from another list in python

From Dev

Getting indices of elements that are in another list in numpy

From Dev

Getting indices of elements that are in another list in numpy

From Dev

accessing elements of a tensor with another list of indices in tensorflow

From Dev

Counting occurrences of elements from one list in another list

From Dev

Remove elements of one list from another list using "while"

From Dev

Summing up the elements in a linked list recursively in C++

Related Related

  1. 1

    Haskell: summing up the elements of a list

  2. 2

    Haskell: summing up the elements of a list

  3. 3

    Remove certain elements in one list based on condition from another list

  4. 4

    Python: One liner for summing up elements in a list meeting a condition

  5. 5

    Repeat elements in one list based on elements from another

  6. 6

    Repeat elements in one list based on elements from another

  7. 7

    Filter Elements from a list based on another list

  8. 8

    Remove elements from a list, using another list as indices

  9. 9

    Check for elements in one list from another list

  10. 10

    Summing up every alternate 4 elements in list

  11. 11

    How to modify the elements of one list based on another

  12. 12

    match element of a list based on another list in python where elements of one list is the substring of elements of another list

  13. 13

    Remove parts of a list based on another list of indices

  14. 14

    Finding the indices of the values of one list in another list

  15. 15

    Finding elements from a list included in another one

  16. 16

    C: moving elements from one list to another

  17. 17

    Maximum of element from List based on the another one

  18. 18

    Maximum of element from List based on the another one

  19. 19

    Summing elements of a list in r

  20. 20

    Summing List Elements

  21. 21

    How to remove elements from a list with lambda based on another list

  22. 22

    Python: Deleting a list element based on elements from another list

  23. 23

    Sum of specific elements of a list based on indexes from another list in python

  24. 24

    Getting indices of elements that are in another list in numpy

  25. 25

    Getting indices of elements that are in another list in numpy

  26. 26

    accessing elements of a tensor with another list of indices in tensorflow

  27. 27

    Counting occurrences of elements from one list in another list

  28. 28

    Remove elements of one list from another list using "while"

  29. 29

    Summing up the elements in a linked list recursively in C++

HotTag

Archive