In Octave if I want to create an array containing an irregular, non-linear set of numbers (e.g. 12, then from 20 to 95 in steps of 5, then 100 to 190 in steps of 10, then 200 to 500 in steps of 25) I can just do:
在八度空间中,如果我想创建一个包含不规则的、非线性的数字集合的数组(例如12,然后从20到95的5步,然后100到190的10步,然后200到500的25步)我可以这样做:
octave:1> nxtals = [12, 20:5:95, 100:10:190, 200:25:500]
nxtals =
Columns 1 through 13:
12 20 25 30 35 40 45 50 55 60 65 70 75
Columns 14 through 26:
80 85 90 95 100 110 120 130 140 150 160 170 180
Columns 27 through 39:
190 200 225 250 275 300 325 350 375 400 425 450 475
Column 40:
500
Is there an elegant, Pythonic (perhaps NumPythonic) equivalent in Numpy/Python3? If I do a list of ranges in Python I get an error:
在Numpy/Python3中是否有一个优雅的、python的(也许是NumPythonic的)等价词?如果我在Python中做一个范围列表,我会得到一个错误:
>>> q=[12, list(range(20, 50, 5)), list(range(50, 100, 10)),list(range(200,501,25))]
>>> numpy.array(q)
Traceback (most recent call last):
File "<pyshell#55>", line 1, in <module>
numpy.array(q)
ValueError: setting an array element with a sequence.
I suspect I need to use something like numpy.ravel()
but can't seem to get it to work.
我怀疑我需要使用numpi .ravel()之类的东西,但似乎无法让它正常工作。
2 个解决方案
#1
4
Numpy defines an object called r_
that can do this. It uses the __getitem__
method to allow the use of the slice notation start:stop:step
to assemble sequences, so instead of calling it, you use square brackets. For example:
Numpy定义了一个名为r_的对象,它可以做到这一点。它使用__getitem__方法允许使用slice符号开始:停止:步骤来组装序列,所以不要调用它,而是使用方括号。例如:
In [1]: nxtals = np.r_[12, 20:100:5, 100:200:10, 200:525:25]
In [2]: nxtals
Out[2]:
array([ 12, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75,
80, 85, 90, 95, 100, 110, 120, 130, 140, 150, 160, 170, 180,
190, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500])
#2
1
You can use np.hstack
to stack those input lists horizontally into a single 1D array -
你可以用np。将这些输入列表水平地堆叠成一个一维数组-
import numpy as np
q = np.hstack( [[12], list(range(20, 50, 5)), list(range(50, 100, 10)),
list(range(200,501,25)) ] )
Sample run -
样本运行-
In [295]: q = np.hstack( [[12], list(range(20, 50, 5)), list(range(50, 100, 10)),
...: list(range(200,501,25)) ] )
In [296]: q
Out[296]:
array([ 12, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 200,
225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500])
#1
4
Numpy defines an object called r_
that can do this. It uses the __getitem__
method to allow the use of the slice notation start:stop:step
to assemble sequences, so instead of calling it, you use square brackets. For example:
Numpy定义了一个名为r_的对象,它可以做到这一点。它使用__getitem__方法允许使用slice符号开始:停止:步骤来组装序列,所以不要调用它,而是使用方括号。例如:
In [1]: nxtals = np.r_[12, 20:100:5, 100:200:10, 200:525:25]
In [2]: nxtals
Out[2]:
array([ 12, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75,
80, 85, 90, 95, 100, 110, 120, 130, 140, 150, 160, 170, 180,
190, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500])
#2
1
You can use np.hstack
to stack those input lists horizontally into a single 1D array -
你可以用np。将这些输入列表水平地堆叠成一个一维数组-
import numpy as np
q = np.hstack( [[12], list(range(20, 50, 5)), list(range(50, 100, 10)),
list(range(200,501,25)) ] )
Sample run -
样本运行-
In [295]: q = np.hstack( [[12], list(range(20, 50, 5)), list(range(50, 100, 10)),
...: list(range(200,501,25)) ] )
In [296]: q
Out[296]:
array([ 12, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 200,
225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500])