【问题标题】:Python2.7 TypeError: 'float' object cannot be interpreted as an indexPython2.7 TypeError:'float'对象不能被解释为索引
【发布时间】:2018-05-14 11:30:23
【问题描述】:

我只是在 github 上克隆一个 repo inconvergent/hyphae 以重用 hyphae.py 并从 python2.6 更新到 python2.7(然后是 3.5)我修复了一些由于更新而导致的错误,但我仍然有 TypeError 用于现在。

完成

python2.6 更新为pyton2.7.13,添加() 毕竟print

修复

File "/home/user/hyphae/hyphae.py", line 378
main()
^
IndentationError: expected an indented block    

通过添加 2 个空格

python2.7 /home/user/hyphae/hyphae.py 何时使用终端

()
('filename', 'generations_a')
('SIZE', 15000)
('ZONEWIDTH', 80.0)
('RAD', 0.002666666666666667)
('ZONES', 187)
('one', 6.666666666666667e-05)
Traceback (most recent call last):
File "/home/user/hyphae/hyphae.py", line 378, in <module>
main()
File "/home/user/hyphae/hyphae.py", line 203, in main
R = np.zeros(NMAX,'float')
TypeError: 'float' object cannot be interpreted as an index

我在 stackoverflow 中被问到关于 TypeError: 'float' object cannot be interpreted as an index 的问题并不多,例如 Herethere,但作为我这样的菜鸟,我不知道如何解决这个问题。

【问题讨论】:

    标签: python python-2.7 typeerror


    【解决方案1】:
    NMAX = 2*1e7 # maxmimum number of nodes
    

    NMAX 是一个浮点数。在旧版本的 numpy 中,您可以使用浮点数作为参数调用 zeros。但这种行为已被弃用,并且在最新版本中不再有效。

    尝试将 NMAX 转换为整数。

    NMAX = int(2*1e7) # maxmimum number of nodes
    

    【讨论】:

    • 哇,非常感谢!!!!它现在完美运行。您对最新版本的此更改有更多解释吗?在看到您的解决方案中的“如何”后,我想了解“为什么”。
    猜你喜欢
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    相关资源
    最近更新 更多