简体   繁体   中英

How to sum value of integers based on position?

The situation is as followed. I want to sum and eventually calculate their average of specific values based on their positions. So far I have tried many different things and I can come up with the following code, I can't seem to figure out how to match these different positions with their belonging values.

    count_pos = 0

    for character in score:
        asci = ord(character)
        count_pos += 1
        print(count_pos,asci)

        if asci == 10 :
           count_pos = 0

print asci generates the following output:

1   35

2   52

3   61

4   68

5   70

6   70




1   35

2   49

3   61

4   68

5   68

6   70

The numbers 1-6 are the positions and the other integers are the values belonging to this value. So what I basically am trying to do is to sum the value of position 1 (35+35) which should give me : 70, and the sum of the values of position 2 should give me (52+49) : 101 and this for all positions.

The only thing so far I thought about was comparing the counter like this:

    if count_pos == count_pos:
       #Do calculation

NOTE: This is just a part of the data. The real data goes on like this with more than 1000 of these counting and not just 2 like displayed here.

If you have the two lists to be added in two lists you may do this :

Using zip :

[x + y for x, y in zip(List1, List2)]

or

zipped_list = zip(List1,List2)
print([sum(item) for item in zipped_list])

Eg: If the lists were,

List1=[1, 2, 3]
List2=[4, 5, 6]

Output would be : [5, 7, 9]

Using Numpy:

import numpy as np
all = [list1,list2,list3 ...]
result = sum(map(np.array, all))

Eg:

>>> li=[1,3]
>>> li1=[1,3]
>>> li2=[1,3]
>>> li3=[1,3]
>>> import numpy as np
>>> all=[li,li1,li2,li3]
>>> mylist = sum(map(np.array, all))
>>> mylist
array([ 4, 12])

Solution

This would work:

from collections import defaultdict

score = '#4=DFF\n#1=DDF\n'
res = defaultdict(int)
for entry in score.splitlines():
    for pos, char in enumerate(entry, 1):
        res[pos] += ord(char)

Now:

>>> res
defaultdict(int, {1: 70, 2: 101, 3: 122, 4: 136, 5: 138, 6: 140})
>>> res[1]
70
>>> res[2]
101

In Steps

Your score string looks like this (extracted from your asci numbers):

score = '#4=DFF\n#1=DDF\n'

Instead of looking for asci == 10 , just split at new line characters with the string method splitlines() .

The defaultdict from the module collections gives you a dictionary that you can initiate with a function. We use int() here. That will call int() if we access a key does not exist. So, if you do:

res[pos] += ord(char)

and the key pos does not exit yet, it will call int() , which gives a 0 and you can add your number to it. The next time around, if the number of pos is already a key in your dictionary, you will get the value and you add to it, summing up the value for each position.

The enumerate here:

for pos, char in enumerate(entry, 1):

gives you the position in each row named pos , starting with 1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM