Home > Blockchain >  Generate numpy.arrange from a list
Generate numpy.arrange from a list

Time:03-06

I have the following list list_a=[4.0, 8.0, 0.5] and i need to get numpy.arange(4.0, 8.0, 0.5)

Issue is i tried to strip the square brackets from my list and place into the numpy.arrange() but it copied as a string

import numpy

list_a=[4.0, 8.0, 0.5]
list_a_strip=str(list_a)[1:-1]

print(f"numpy_arange: {numpy.arange(list_a_strip)}")

Error is TypeError: unsupported operand type(s) for -: 'str' and 'int'

But when i manually put the values in, all looks good

import numpy

list_a=[4.0, 8.0, 0.5]
list_a_strip=str(list_a)[1:-1]
​
print(f"numpy_arange: {numpy.arange(4.0, 8.0, 0.5)}")
numpy_arange: [4.  4.5 5.  5.5 6.  6.5 7.  7.5]

So how i can generate the numpy.arange() from a list?

CodePudding user response:

You can use argument unpacking to send the arguments to np.arange in the order you have them in the list.

import numpy as np

list_a=[4.0, 8.0, 0.5]
np.arange(*list_a)

# returns: 
array([4. , 4.5, 5. , 5.5, 6. , 6.5, 7. , 7.5])
  • Related