【发布时间】:2013-08-13 09:58:15
【问题描述】:
我很清楚 there are differences between lists and tuples 和 tuples aren't just constant lists,但很少有例子表明 代码 对两者的处理方式不同(与编码约定相反),所以我(草率地)交替使用它们。
然后我遇到了一个他们给出完全不同行为的案例:
>>> import numpy as np
>>> a = np.arange(9).reshape(3,3)
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> idx = (1,1)
>>> a[idx]
4
>>> idx = [1,1]
>>> a[idx]
array([[3, 4, 5],
[3, 4, 5]])
有人能解释一下这里发生了什么吗?更重要的是,这个陷阱在 scipy 中还出现在哪里?
【问题讨论】: