Python 2.7 指定Numpy数组中的元素

Python 2.7 指定Numpy数组中的元素,python-2.7,numpy,spyder,Python 2.7,Numpy,Spyder,我想创建numpy数组,它接受[0,10]范围内的值,每个元素之间的除法为0.1。我该怎么做呢?希望我的要求足够清楚。使用 从文档中: numpy.arange([start],stop[,step],dtype=None)以均匀间隔返回 给定间隔内的值 例如: >>> np.arange(3) array([0, 1, 2]) >>> np.arange(3.0) array([ 0., 1., 2.]) >>> np.arange(3

我想创建numpy数组,它接受[0,10]范围内的值,每个元素之间的除法为0.1。我该怎么做呢?希望我的要求足够清楚。

使用

从文档中:

numpy.arange([start],stop[,step],dtype=None)以均匀间隔返回 给定间隔内的值

例如:

>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3.0)
array([ 0.,  1.,  2.])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5])
给你:

 np.arange(0,10,0.1)
使用

从文档中:

numpy.arange([start],stop[,step],dtype=None)以均匀间隔返回 给定间隔内的值

例如:

>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3.0)
array([ 0.,  1.,  2.])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5])
给你:

 np.arange(0,10,0.1)
使用

从文档中:

numpy.arange([start],stop[,step],dtype=None)以均匀间隔返回 给定间隔内的值

例如:

>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3.0)
array([ 0.,  1.,  2.])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5])
给你:

 np.arange(0,10,0.1)
使用

从文档中:

numpy.arange([start],stop[,step],dtype=None)以均匀间隔返回 给定间隔内的值

例如:

>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3.0)
array([ 0.,  1.,  2.])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5])
给你:

 np.arange(0,10,0.1)

除了@agconti answer,您还可以使用所谓的linspace

代码:

import numpy

a = numpy.linspace(0, 10, num = 101)  # num -> number of elements
print a
[  0.    0.1   0.2   0.3   0.4   0.5   0.6   0.7   0.8   0.9   1.    1.1
1.2   1.3   1.4   1.5   1.6   1.7   1.8   1.9   2.    2.1   2.2   2.3
2.4   2.5   2.6   2.7   2.8   2.9   3.    3.1   3.2   3.3   3.4   3.5
3.6   3.7   3.8   3.9   4.    4.1   4.2   4.3   4.4   4.5   4.6   4.7
4.8   4.9   5.    5.1   5.2   5.3   5.4   5.5   5.6   5.7   5.8   5.9
6.    6.1   6.2   6.3   6.4   6.5   6.6   6.7   6.8   6.9   7.    7.1
7.2   7.3   7.4   7.5   7.6   7.7   7.8   7.9   8.    8.1   8.2   8.3
8.4   8.5   8.6   8.7   8.8   8.9   9.    9.1   9.2   9.3   9.4   9.5
9.6   9.7   9.8   9.9  10. ]
输出:

import numpy

a = numpy.linspace(0, 10, num = 101)  # num -> number of elements
print a
[  0.    0.1   0.2   0.3   0.4   0.5   0.6   0.7   0.8   0.9   1.    1.1
1.2   1.3   1.4   1.5   1.6   1.7   1.8   1.9   2.    2.1   2.2   2.3
2.4   2.5   2.6   2.7   2.8   2.9   3.    3.1   3.2   3.3   3.4   3.5
3.6   3.7   3.8   3.9   4.    4.1   4.2   4.3   4.4   4.5   4.6   4.7
4.8   4.9   5.    5.1   5.2   5.3   5.4   5.5   5.6   5.7   5.8   5.9
6.    6.1   6.2   6.3   6.4   6.5   6.6   6.7   6.8   6.9   7.    7.1
7.2   7.3   7.4   7.5   7.6   7.7   7.8   7.9   8.    8.1   8.2   8.3
8.4   8.5   8.6   8.7   8.8   8.9   9.    9.1   9.2   9.3   9.4   9.5
9.6   9.7   9.8   9.9  10. ]

除了@agconti answer,您还可以使用所谓的linspace

代码:

import numpy

a = numpy.linspace(0, 10, num = 101)  # num -> number of elements
print a
[  0.    0.1   0.2   0.3   0.4   0.5   0.6   0.7   0.8   0.9   1.    1.1
1.2   1.3   1.4   1.5   1.6   1.7   1.8   1.9   2.    2.1   2.2   2.3
2.4   2.5   2.6   2.7   2.8   2.9   3.    3.1   3.2   3.3   3.4   3.5
3.6   3.7   3.8   3.9   4.    4.1   4.2   4.3   4.4   4.5   4.6   4.7
4.8   4.9   5.    5.1   5.2   5.3   5.4   5.5   5.6   5.7   5.8   5.9
6.    6.1   6.2   6.3   6.4   6.5   6.6   6.7   6.8   6.9   7.    7.1
7.2   7.3   7.4   7.5   7.6   7.7   7.8   7.9   8.    8.1   8.2   8.3
8.4   8.5   8.6   8.7   8.8   8.9   9.    9.1   9.2   9.3   9.4   9.5
9.6   9.7   9.8   9.9  10. ]
输出:

import numpy

a = numpy.linspace(0, 10, num = 101)  # num -> number of elements
print a
[  0.    0.1   0.2   0.3   0.4   0.5   0.6   0.7   0.8   0.9   1.    1.1
1.2   1.3   1.4   1.5   1.6   1.7   1.8   1.9   2.    2.1   2.2   2.3
2.4   2.5   2.6   2.7   2.8   2.9   3.    3.1   3.2   3.3   3.4   3.5
3.6   3.7   3.8   3.9   4.    4.1   4.2   4.3   4.4   4.5   4.6   4.7
4.8   4.9   5.    5.1   5.2   5.3   5.4   5.5   5.6   5.7   5.8   5.9
6.    6.1   6.2   6.3   6.4   6.5   6.6   6.7   6.8   6.9   7.    7.1
7.2   7.3   7.4   7.5   7.6   7.7   7.8   7.9   8.    8.1   8.2   8.3
8.4   8.5   8.6   8.7   8.8   8.9   9.    9.1   9.2   9.3   9.4   9.5
9.6   9.7   9.8   9.9  10. ]

除了@agconti answer,您还可以使用所谓的linspace

代码:

import numpy

a = numpy.linspace(0, 10, num = 101)  # num -> number of elements
print a
[  0.    0.1   0.2   0.3   0.4   0.5   0.6   0.7   0.8   0.9   1.    1.1
1.2   1.3   1.4   1.5   1.6   1.7   1.8   1.9   2.    2.1   2.2   2.3
2.4   2.5   2.6   2.7   2.8   2.9   3.    3.1   3.2   3.3   3.4   3.5
3.6   3.7   3.8   3.9   4.    4.1   4.2   4.3   4.4   4.5   4.6   4.7
4.8   4.9   5.    5.1   5.2   5.3   5.4   5.5   5.6   5.7   5.8   5.9
6.    6.1   6.2   6.3   6.4   6.5   6.6   6.7   6.8   6.9   7.    7.1
7.2   7.3   7.4   7.5   7.6   7.7   7.8   7.9   8.    8.1   8.2   8.3
8.4   8.5   8.6   8.7   8.8   8.9   9.    9.1   9.2   9.3   9.4   9.5
9.6   9.7   9.8   9.9  10. ]
输出:

import numpy

a = numpy.linspace(0, 10, num = 101)  # num -> number of elements
print a
[  0.    0.1   0.2   0.3   0.4   0.5   0.6   0.7   0.8   0.9   1.    1.1
1.2   1.3   1.4   1.5   1.6   1.7   1.8   1.9   2.    2.1   2.2   2.3
2.4   2.5   2.6   2.7   2.8   2.9   3.    3.1   3.2   3.3   3.4   3.5
3.6   3.7   3.8   3.9   4.    4.1   4.2   4.3   4.4   4.5   4.6   4.7
4.8   4.9   5.    5.1   5.2   5.3   5.4   5.5   5.6   5.7   5.8   5.9
6.    6.1   6.2   6.3   6.4   6.5   6.6   6.7   6.8   6.9   7.    7.1
7.2   7.3   7.4   7.5   7.6   7.7   7.8   7.9   8.    8.1   8.2   8.3
8.4   8.5   8.6   8.7   8.8   8.9   9.    9.1   9.2   9.3   9.4   9.5
9.6   9.7   9.8   9.9  10. ]

除了@agconti answer,您还可以使用所谓的linspace

代码:

import numpy

a = numpy.linspace(0, 10, num = 101)  # num -> number of elements
print a
[  0.    0.1   0.2   0.3   0.4   0.5   0.6   0.7   0.8   0.9   1.    1.1
1.2   1.3   1.4   1.5   1.6   1.7   1.8   1.9   2.    2.1   2.2   2.3
2.4   2.5   2.6   2.7   2.8   2.9   3.    3.1   3.2   3.3   3.4   3.5
3.6   3.7   3.8   3.9   4.    4.1   4.2   4.3   4.4   4.5   4.6   4.7
4.8   4.9   5.    5.1   5.2   5.3   5.4   5.5   5.6   5.7   5.8   5.9
6.    6.1   6.2   6.3   6.4   6.5   6.6   6.7   6.8   6.9   7.    7.1
7.2   7.3   7.4   7.5   7.6   7.7   7.8   7.9   8.    8.1   8.2   8.3
8.4   8.5   8.6   8.7   8.8   8.9   9.    9.1   9.2   9.3   9.4   9.5
9.6   9.7   9.8   9.9  10. ]
输出:

import numpy

a = numpy.linspace(0, 10, num = 101)  # num -> number of elements
print a
[  0.    0.1   0.2   0.3   0.4   0.5   0.6   0.7   0.8   0.9   1.    1.1
1.2   1.3   1.4   1.5   1.6   1.7   1.8   1.9   2.    2.1   2.2   2.3
2.4   2.5   2.6   2.7   2.8   2.9   3.    3.1   3.2   3.3   3.4   3.5
3.6   3.7   3.8   3.9   4.    4.1   4.2   4.3   4.4   4.5   4.6   4.7
4.8   4.9   5.    5.1   5.2   5.3   5.4   5.5   5.6   5.7   5.8   5.9
6.    6.1   6.2   6.3   6.4   6.5   6.6   6.7   6.8   6.9   7.    7.1
7.2   7.3   7.4   7.5   7.6   7.7   7.8   7.9   8.    8.1   8.2   8.3
8.4   8.5   8.6   8.7   8.8   8.9   9.    9.1   9.2   9.3   9.4   9.5
9.6   9.7   9.8   9.9  10. ]


我最初考虑过使用它,但认为它在概念上更难,因为你必须知道数组中的元素数。但是我很高兴你把它包括进来是为了完整,+1。这取决于你用它做什么。你想用数组做什么?另外,对于非python用户来说,这可能更具语义:
np.linspace(0,1100,endpoint=False)
。我最初考虑过使用它,但从概念上讲,它更难,因为你必须知道数组中的元素数。但是我很高兴你把它包括进来是为了完整,+1。这取决于你用它做什么。你想用数组做什么?另外,对于非python用户来说,这可能更具语义:
np.linspace(0,1100,endpoint=False)
。我最初考虑过使用它,但从概念上讲,它更难,因为你必须知道数组中的元素数。但是我很高兴你把它包括进来是为了完整,+1。这取决于你用它做什么。你想用数组做什么?另外,对于非python用户来说,这可能更具语义:
np.linspace(0,1100,endpoint=False)
。我最初考虑过使用它,但从概念上讲,它更难,因为你必须知道数组中的元素数。但是我很高兴你把它包括进来是为了完整,+1。这取决于你用它做什么。你想用数组做什么?另外,对于非python用户来说,这可能更具语义:
np.linspace(0,1100,endpoint=False)
。下层投票人是否愿意发表评论?我的答案是有效的。好吧,检查arange文档“当使用非整数步长时,例如0.1,结果通常不一致。最好在这些情况下使用linspace。”显然我的答案是有效的。仍然会生成正确的结果。当我评论@kitians answer时,我认为这在概念上可能比ops的linespace更容易。简单地说,不。这个答案甚至不能概括一点。如果你推广它,你可能会得到腐败的结果。而且它不如linspace精确(尽管速度更快)。所以,不管它是否有效,我认为这是一个糟糕的建议。NP.ARange()是否产生了一个NUMPY数组?下级选民会不会愿意发表评论?我的答案是有效的。好吧,检查arange文档“当使用非整数步长时,例如0.1,结果通常不一致。最好在这些情况下使用linspace。”显然我的答案是有效的。仍然会生成正确的结果。当我评论@kitians answer时,我认为这在概念上可能比ops的linespace更容易。简单地说,不。这个答案甚至不能概括一点。如果你推广它,你可能会得到腐败的结果。而且它不如linspace精确(尽管速度更快)。所以,不管它是否有效,我认为这是一个糟糕的建议。NP.ARange()是否产生了一个NUMPY数组?下级选民会不会愿意发表评论?我的答案是有效的。好吧,检查arange文档“当使用非整数步长时,例如0.1,结果通常不一致。最好在这些情况下使用linspace。”显然我的答案是有效的。仍然会生成正确的结果。当我评论@kitians answer时,我认为这在概念上可能比ops的linespace更容易。简单地说,不。这个答案甚至不能概括一点。如果你推广它,你可能会得到腐败的结果。而且它不如linspace精确(尽管速度更快)。所以,不管它是否有效,我认为这是一个糟糕的建议。NP.ARange()是否产生了一个NUMPY数组?下级选民会不会愿意发表评论?我的答案是有效的。好吧,检查arange文档“当使用非整数步长时,例如0.1,结果通常不一致。最好在这些情况下使用linspace。”显然我的答案是有效的。仍然会生成正确的结果。当我评论@kitians answer时,我认为这在概念上可能比ops的linespace更容易。简单地说,不。这个答案甚至不能概括一点。如果你推广它,你可能会得到腐败的结果。而且它不如linspace精确(尽管速度更快)。因此,不管它是否有效,我认为这是一个糟糕的建议。NP.ARange()是否产生了一个NUMPY数组?