Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python范围与np.intc_Python_Numpy_Integer_Range - Fatal编程技术网

Python范围与np.intc

Python范围与np.intc,python,numpy,integer,range,Python,Numpy,Integer,Range,昨天我在这里问了一个关于pythonrange()的奇怪行为的问题。 我不小心用了range()而不是np.arange()。但这取决于 输入int类型它是否适用于数学运算。 这是在一个巨大的背景下,所以我简化了这个问题 将numpy导入为np """ 行得通 """ x1=np.intc(545) x2=np.intc(1048) x_轴=范围(x1,x2) test=(x_轴-x1)#应引发异常,但有效 类型(x_轴[0])#numpy.int32/intc """ 不起作用 """ t1=

昨天我在这里问了一个关于python
range()
的奇怪行为的问题。 我不小心用了
range()
而不是
np.arange()
。但这取决于 输入int类型它是否适用于数学运算。 这是在一个巨大的背景下,所以我简化了这个问题

将numpy导入为np
"""
行得通
"""
x1=np.intc(545)
x2=np.intc(1048)
x_轴=范围(x1,x2)
test=(x_轴-x1)#应引发异常,但有效
类型(x_轴[0])#numpy.int32/intc
"""
不起作用
"""
t1=545
t2=1048
t_轴=范围(t1,t2)
test2=(t_轴-t1)#抛出类型错误-:'range'和'int'不支持的操作数类型
背景: 使用intc数据类型的原因是,我的程序执行一些其他任务,如peakfinding等,我使用一些列表理解来从计算出的峰值生成列表。在其他一些步骤之后,我使用这个峰值列表中的x1和x2(这显然不是一个正常的int列表)。因此,我昨天更改了代码并生成了一个新列表,然后出现了异常。我猜numpy/scipy在内部使用intc


有人能解释这种行为吗?

Python序列和标量的行为不同于numpy 1d数组和标量。让我们看几个基本的例子,了解其中的诀窍(如果很匆忙,可以跳到底部):

#定义我们的测试对象
a_范围=范围(2,6)
a_列表=列表(a_范围)
a_数组=np.array(a_列表)
#范围不会增加
a_范围+a_范围
#回溯(最近一次呼叫最后一次):
#文件“”,第1行,在
#TypeError:+:“range”和“range”的操作数类型不受支持
#列表确实可以添加,但语义不同。。。
a_列表+a_列表
# [2, 3, 4, 5, 2, 3, 4, 5]
# ... 来自numpy数组
a_数组+a_数组
#数组([4,6,8,10])
#如果我们混合类型会发生什么?
#范围和列表不能混合:
a_范围+a_列表
#回溯(最近一次呼叫最后一次):
#文件“”,第1行,在
#TypeError:不支持+:“范围”和“列表”的操作数类型
#但一旦涉及到一个numpy对象,它就会“获胜”:
a_范围+a_数组
#数组([4,6,8,10])
列表+数组
#数组([4,6,8,10])
#标量呢?
py_标量=3
np\u标量=np.int64(py\u标量)
#同样,在纯python中,不能向范围中添加内容。。。
a_范围+py_标量
#回溯(最近一次呼叫最后一次):
#文件“”,第1行,在
#TypeError:不支持+:“range”和“int”的操作数类型
#或者一份清单
a_列表+py_标量
#回溯(最近一次呼叫最后一次):
#文件“”,第1行,在
#TypeError:只能将列表(而不是“int”)连接到列表
#但您可以将Python或numpy标量添加到numpy对象
a_数组+py_标量
#数组([5,6,7,8])
a_数组+np_标量
#数组([5,6,7,8])
#现在,如果标量是一个numpy对象,它同样“获胜”:
a_范围+np_标量
#数组([5,6,7,8])
a_列表+np_标量
#数组([5,6,7,8])

总之,Python序列和numpy 1d数组具有不同的语义,特别是使用诸如“+”或“-”或“*”之类的二进制运算符。如果至少有一个操作数是numpy对象(数组或标量),numpy将获胜:非numpy对象将被转换(平面序列变为1d数组),numpy语义将适用。

感谢您的详细回复!现在我对这种行为有了更多的了解;)
# define our test objects
a_range = range(2,6)
a_list = list(a_range)
a_array = np.array(a_list)

# ranges don't add
a_range+a_range
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: unsupported operand type(s) for +: 'range' and 'range'

# lists do add but with different semantics ...
a_list+a_list
# [2, 3, 4, 5, 2, 3, 4, 5]

# ... from numpy arrays
a_array+a_array
# array([ 4,  6,  8, 10])

# what happens if we mix types?

# range and list don't mix:
a_range+a_list
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: unsupported operand type(s) for +: 'range' and 'list'

# but as soon as there is a numpy object involved it "wins":
    
a_range+a_array
# array([ 4,  6,  8, 10])

a_list+a_array 
# array([ 4,  6,  8, 10])

# How about scalars?

py_scalar = 3
np_scalar = np.int64(py_scalar)

# again, in pure python you cannot add something to a range ...
a_range+py_scalar
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: unsupported operand type(s) for +: 'range' and 'int'

# or a list
a_list+py_scalar
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: can only concatenate list (not "int") to list

# but you can add a Python or numpy scalar to a numpy object
a_array+py_scalar
# array([5, 6, 7, 8])

a_array+np_scalar
# array([5, 6, 7, 8])

# Now if the scalar is a numpy object, again, it "wins":

a_range+np_scalar
# array([5, 6, 7, 8])

a_list+np_scalar
# array([5, 6, 7, 8])