pre*_*lic 17 python numpy slice
我熟悉切片,我只是无法绕过这个,我尝试改变一些值来试图说明发生了什么,但这对我没有意义.
无论如何,这是一个例子:
l = [[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 1]]
print l[:,0:2]
Run Code Online (Sandbox Code Playgroud)
导致:
[[0, 0], [0, 1] [1, 0], [1, 1]]
Run Code Online (Sandbox Code Playgroud)
我试图将其翻译为"从索引0切换到0,2,递增2"这对我来说毫无意义.
小智 11
只有当 'l' 是一个 numpy 数组时,才对我说的那样工作。对于 'l' 作为常规列表,它会引发错误(Python 3.6):
>>> l
[[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 1]]
>>> print (l[:,0:2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not tuple
>>> l=np.array(l)
>>> l
array([[0, 0, 0],
[0, 1, 0],
[1, 0, 0],
[1, 1, 1]])
>>> print (l[:,0:2])
[[0 0]
[0 1]
[1 0]
[1 1]]
>>>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23765 次 |
| 最近记录: |