Python: 这些列表索引代表什么意思?

huangapple go评论88阅读模式
英文:

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]]

huangapple
  • 本文由 发表于 2023年6月13日 02:57:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定