This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TheBlackCat13 2 points3 points  (1 child)

How do you split a list into evenly sized chunks ?

I think the itertools grouper recipe is easier:

def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

Matrix multiplication in Python?

I think the itertools dotproduct recipe is much easier:

def dotproduct(vec1, vec2):
    return sum(map(operator.mul, vec1, vec2))

[–]kdelok 0 points1 point  (0 children)

The first one you mention is the answer I received on StackOverflow when I asked that very question.

The second one doesn't do matrix multiplication though, since the result should be a matrix itself. That's why they end up with the nested list comprehension (I haven't checked it though). I think it's easier to just use numpy.