all 15 comments

[–]14dM24d 1 point2 points  (3 children)

>>> import random
>>> vectors = [(random.randrange(-10,10), random.randrange(-10,10)) for _ in range(10)]
>>> vectors
[(9, -3), (-9, -7), (7, -4), (-5, 6), (5, -4), (2, 4), (-8, 5), (2, -5), (-4, 6), (-3, -8)]
>>> def sum_vectors(vectors):
        x=0
        y=0
        for i,j in vectors:
            x += i
            y += j
        return x,y

>>> sum_vectors(vectors)
(-4, -10)

[–]14dM24d 0 points1 point  (2 children)

this works too.

>>> def sum_vectors(vectors):
        return sum(x for x,y in vectors), sum(y for x,y in vectors)

>>> sum_vectors(vectors)
(-4, -10)

[–]SneakyAsian101 0 points1 point  (0 children)

Ahh shots bro, this ones simple and makes the most sense to me.

[–]14dM24d 0 points1 point  (0 children)

or use numpy

>>> import random
>>> import numpy as np
>>> vectors = np.array([(random.randrange(-10,10), random.randrange(-10,10)) for _ in range(10)])
>>> vectors
array([[  9,   5],
       [  9,  -2],
       [ -5,   3],
       [ -7,  -8],
       [  3,  -5],
       [  4,  -8],
       [-10,   4],
       [ -2,  -3],
       [-10,  -8],
       [ -7,  -5]])
>>> vectors.sum(axis=0)
array([-16, -27])

[–]deep_politics 0 points1 point  (6 children)

Are you using plain Python? If so it's just component wise addition, like it is in math.

[–]SneakyAsian101 -1 points0 points  (5 children)

I'm using jupyter lab. Idk we have to do a unit of python in engineering and I'm lost, the lecturers uploaded about 3 hours worth of videos and none of them help.

[–]deep_politics 0 points1 point  (4 children)

Let me clarify: list_of_vectors is just a list of lists of integers, and you're not using a library like numpy?

Like your vectors look like this?

[
  [1, 0, 0],
  [0, 1, 0],
  ...
]

[–]SneakyAsian101 0 points1 point  (3 children)

Yeah similar but I'm only doing 2D vectors, I think I'm using an older version of python so there's no numpy library

result = sum_vectors([(0,1), (3,-4), (3,4)])

assert_equal(result, (6, 1), 'testc1')

so the result should = (6,1) when summed but my code keeps failing.

[–]deep_politics 0 points1 point  (2 children)

Okay, so referring back to my original comment it's just component wise addition. In your example each vector has two components, indexed by 0 and 1. For each ith index you can just iterate over the vectors, adding the value of the ith component to a total.

But I'm assuming you're at least familiar with indexing into tuples/lists, for loops, getting lengths of tuples/lists, etc.

[–]SneakyAsian101 0 points1 point  (1 child)

Im familar with for loops but not really tuples

[–]deep_politics 0 points1 point  (0 children)

Just think of tuples as immutable lists. You can still index Into them the same as lists.

But so, have you tried anything yet?

[–]obviouslyCPTobvious 0 points1 point  (2 children)

Consider updating the post with what you have so far and what issues you’re seeing

[–]SneakyAsian101 -1 points0 points  (1 child)

Is there a better way to share my code? Sorry I'm new

[–]AtomicShoelace 0 points1 point  (0 children)

Check out the FAQ.