Bob John
Bob John

Reputation: 3888

Creating multiple vectors

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.

Upvotes: 1

Views: 880

Answers (3)

Óscar López
Óscar López

Reputation: 236114

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])

Upvotes: 1

jjwchoy
jjwchoy

Reputation: 1908

This should work

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

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304375

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])

Upvotes: 1

Related Questions