简体   繁体   中英

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:

[]

meanwhile doing print a[0][0] outputs(the first Item in the first array within the array): [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:] .

[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] is an index lookup, and [:0:] is slice notation, so they are quite different things.

This print a[0][0] refers to the first element on 2d in the first element of 1d.

This print a[0][:0:] refers to slice notation:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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