【问题标题】:arange: TypeError: unsupported operand type(s) for -: 'list' and 'int'范围:TypeError:不支持的操作数类型 -:'list' 和 'int'
【发布时间】:2014-07-04 09:56:34
【问题描述】:

我正在尝试编写一个非常简单的代码,它只返回一个从 0 到 10 以 0.1 为增量的 x 数组,但不断出现错误:

    x = np.arange[[0,10,0.1,np.float]]
TypeError: 'builtin_function_or_method' object is not subscriptable

这是我的代码:

import numpy as np 
import matplotlib.pyplot as plt 

results = {} # creating an empty dictionary 

f = open (r'D:\Work\Thesis\TEST_figures\telemac_comparison_1', 'w')

f.write ('Distance'+'Free surface')

result = [[], []]
x = np.arange([0,10,0.1,np.float])
print x 

正如你所看到的,它显然还没有完成,但我想现在对这一行进行排序。

【问题讨论】:

  • 为什么你标题中的错误与你的回溯中的错误不同?为什么您的回溯又与您声称正在运行的代码不匹配?!
  • @jonrsharpe:如果您将np.float 作为第二个参数传入,第一个参数是列表减去np.float,则会导致该错误。
  • 好吧,我的错误,我包含的第一个错误(TypeError: 'builtin_function_or_method' object is not subscriptable)来自我试图修复我的代码时。谢谢大家的回复,很有用。
  • @user3771983 以后,确保您报告的错误、您提供的回溯和您发布的代码都引用相同版本的代码会很有帮助。最好在发布之前将您的代码减少到 a minimal example,但是如果错误仍然存​​在,您应该对代码进行最终检查并发布特定的回溯

标签: python numpy


【解决方案1】:

不要传入列表,传入单独的参数

x = np.arange(0, 10, 0.1, np.float)

演示:

>>> np.arange(0, 10, 0.1, np.float)
array([ 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])

顺便说一句,问题标题中的错误消息是当您传入一个列表作为第一个参数,np.float 作为第二个参数时引起的:

>>> np.arange([0,10,0.1], np.float)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'type' and 'list'

stopstartstep 值是documented function signature 中的单独参数。

您的回溯与您实际发布的代码不同;这是在函数对象上使用订阅语法(索引),而不是尝试调用它。不要使用np.arange[],它是一个函数对象而不是一个序列。

【讨论】:

  • 很好地抓住了标题中的错误。我认为标题和回溯和代码是三个完全独立的尝试是一个新的记录。
【解决方案2】:

你似乎有两次尝试

  1. np.arange[[...]] 尝试订阅(参见列表索引l[0] 或通过键d['key'] 进行字典访问),这没有任何意义 - arange 是一个函数;和

  2. np.arange([...]) 传递一个列表参数,而 arange 实际上需要(最多)四个单独的参数

相反,您应该这样做:

np.arange(*[...]) # unpack the list to separate arguments

如果您想定义一次参数并重复使用它们,这将很有用,或者:

np.arange(...) # pass as separate arguments

后者的演示:

>>> x = np.arange(0, 10, 0.1, np.float)
>>> x
array([ 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])

以后尝试使用函数时,请考虑reading the documentation

【讨论】:

    猜你喜欢
    • 2012-12-12
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2014-12-28
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多