0

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.

Idan Gelber
  • 195
  • 3
  • 4
  • 11

2 Answers2

3

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.

101
  • 8,514
  • 6
  • 43
  • 69
1

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

Community
  • 1
  • 1
kwarunek
  • 12,141
  • 4
  • 43
  • 48