3

I have a list of numbers go in one row like this:

0
1
0
2
0
4

and it has about thousands of rows

I want to add them up every 3 rows so the results will be like this:

1
6

I have already made the list into individual integer with this line:

k = map(lambda s: s.strip(), k)
integer = map(int, k)

How would I able to do the adding up?

3
  • When you say that you have a list of numbers, do you mean a file, string or an actual list? Commented Jul 31, 2012 at 10:54
  • 2
    Hint: sum plus Alternative way to split a list into groups of n Commented Jul 31, 2012 at 10:55
  • @jamylak I had it in a .txt file, but I have to codes written for opening the files, reading the files and the code above converting them into interger Commented Jul 31, 2012 at 10:56

6 Answers 6

4

Use a grouper, like from Alternative way to split a list into groups of n:

import itertools

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return itertools.izip_longest(*args, fillvalue=fillvalue)

map(sum, grouper(3, interger, 0))

Note that the above code assumes python 2.x; for 3.x you'll need to use itertools.zip_longest instead.

Demo:

>>> example = [0, 1, 0, 2, 0, 4]
>>> map(sum, grouper(3, example, 0))
[1, 6]
Sign up to request clarification or add additional context in comments.

Comments

1

Since you say you already have a list of numbers, then

result = [sum(data[i:i+3])for i in range(0, len(data),3)]

e.g.,

 data = range(1, 16) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

yields

 [6, 15, 24, 33, 42]

1 Comment

@Downvoter happy to correct errors or improve the answer, but a downvote without explantion is not constructive, and worthless to OP, SO or me.
1

This is probably the most pythonic:

>>> my_list = iter(my_list)
>>> map(sum, zip(my_list, my_list, my_list))
[1, 6]

Comments

0

Using itertools grouper recipe

>>> from itertools import izip_longest
>>> def grouper(n, iterable, fillvalue=None):
        "Collect data into fixed-length chunks or blocks"
        # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
        args = [iter(iterable)] * n
        return izip_longest(fillvalue=fillvalue, *args)

>>> with open('data.txt') as fin:
        for g in grouper(3, fin):
            print sum(map(int, g))


1
6

Comments

0

Grouper and all the fancy stuff will work ... but simple list comprehension will do in this case. And it should be nice and fast! :)

>>> q = range(9)
>>> q
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> [sum(q[3*n:3*n+3]) for n in range(len(q)/3)]
[3, 12, 21]

Comments

0

This will skip the remaining numbers that can't fit into a group of three:

>>> def sum_three(iterable_):
...     iterable_ = iter(iterable_)
...     while True:
...         yield next(iterable_) + next(iterable_) + next(iterable_)
...
>>> my_list = [0, 1, 0, 2, 0, 4, 99, 1]
>>> list(sum_three(my_list))
>>> [1, 6]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.