Search code examples
pythonvpython

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.


Solution

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