Creating multiple vectors

Question:

So lets say I have a list of numbers and I want to create a vector out of all of them in the form (x, 0, 0). How would I do this?

hello = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

So when I access, say, hello[2] I get (3, 0, 0) instead of just 3.

Asked By: Bob John

||

Answers:

If you are working with vectors, it’s best to use numpy as it has support for lots of vector operations that Python doesn’t

>>> import numpy as np
>>> hello = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> hello = (hello*np.array([(1,0,0)]*10).transpose()).transpose()
>>> hello[2]
array([3, 0, 0])
>>> hello[2]*3
array([9, 0, 0])
Answered By: John La Rooy

This should work

hello = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_hello = [(n, 0, 0) for n in hello]
Answered By: jjwchoy

Try this, using numpy – "the fundamental package for scientific computing with Python":

import numpy as np
hello = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
hello = [np.array([n, 0, 0]) for n in hello]

The above will produce the results you expect:

>>> hello[2]
array([3, 0, 0])

>>> hello[2] * 3
array([9, 0, 0])
Answered By: Óscar López
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.