pythonarraysdimensional

Multiplying 1D Array in Python


If I have a 1D array in Python for example:

a = (10,20,30,40,50)

How can I multiply this by an integer for example 2 to produce:

b = (20,40,60,80,100)

I have tried:

b = a*2 

But it doesn't seem to do anything.


Solution

  • Use the following:

    >>> b = [2 * i for i in a]
    >>> b
    [20, 40, 60, 80, 100]
    

    a * 2 will duplicate your set:

    >>> a = (10,20,30,40,50)
    >>> a * 2
    (10, 20, 30, 40, 50, 10, 20, 30, 40, 50)