英文:
Python: what do these list indexes mean?
问题
我见过这个语句:
x[:, 0:4]
我对此有些困惑。 有人能解释一下它在列表索引方面的含义吗?
英文:
I have seen this statement:
x[:, 0:4]
I'm a little confused by this. Can anyone explain what it means in terms of indexes into a list?
答案1
得分: -1
这通常在NumPy库中使用,该库提供了用于数组和矩阵的各种功能。
第一个位置的冒号符号表示应选择数组的所有行。
0:4 指定了列范围,意味着将选择索引从0到4(不包括4)的列。
使用 x[:, 0:4] 表示法,所选的子元素集将是:
[[1, 2, 3, 4],
[6, 7, 8, 9],
[11, 12, 13, 14]]
英文:
This is commonly used in the NumPy library, which provides various stuff for arrays and matrices.
The : symbol in the first position indicates that all rows of the array should be included in the selection.
The 0:4 specifies the column range and means that columns with indices from 0 up to, but not including, 4 will be selected.
x = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
Using the x[:, 0:4] notation, the selected subset of elements would be:
[[1, 2, 3, 4],
[6, 7, 8, 9],
[11, 12, 13, 14]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论