简体   繁体   English

为什么打印a [0] [:0:]与打印a [0] [0]给我不同的答案? (Python多维数组)

[英]Why does printing a[0][:0:] give me a different answer than printing a[0][0] ? (Python multi-Dimensional array)

lets say I have array a where: 可以说我在哪里数组:

a = [[1.0,2.5,3.0],[2.0,5.0,3.0]]

why does doing print a[0][:0:] output: 为什么要print a[0][:0:]输出:

[]

meanwhile doing print a[0][0] outputs(the first Item in the first array within the array): [1.0] 同时print a[0][0]输出(数组中第一个数组中的第一项): [1.0]

I eventually want to take moving averages of multi-dimensional arrays. 我最终想要获取多维数组的移动平均值。 I want to make sure I understand the syntax first. 我想确保我先了解语法。

For the same reason that [1, 2, 3][0] gives a different answer to [1, 2, 3][:0:] . 出于同样的原因, [1, 2, 3][0][1, 2, 3][:0:]给出了不同的答案。

[0] will give the element at index 0, [:0:] (or more simply [:0] ) will give all the elements from the start of the list to before the element at index 0 (which is en empty list of course). [0]将给出索引为0的元素, [:0:] (或更简单地为[:0] )将给出从列表开头到索引0的元素之前的所有元素(该列表为空列表)。课程)。

[0] is an index lookup, and [:0:] is slice notation, so they are quite different things. [0]是索引查找, [:0:]是切片符号,因此它们是完全不同的东西。

This print a[0][0] refers to the first element on 2d in the first element of 1d. print a[0][0]引用1d的第一个元素中2d上的第一个元素。

This print a[0][:0:] refers to slice notation: print a[0][:0:]表示切片符号:

a[start:end] # items start through end-1
a[start:]    # items start through the rest of the array
a[:end]      # items from the beginning through end-1
a[:]         # a copy of the whole array

Explain Python's slice notation 解释Python的切片符号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM