python slicing [:-N] [:-N+1]

Viewed 115

I been trying to code the following line but I get the message that operands could not be broadcast together with shapes (0,) (30,) x has a length of 32

  • x[:-N+1] I want to access all elements except the last two
  • x[N:-N] I want to access all elements except the first one and the last one
  • x[N+1:] I want to access all elements except the first
y = x[:-N+1] - 2 * x[N:-N] + x[N+1:]

How should I index x to access those values? I'm new to python so any tips would be appreciated.

1 Answers

Slice except the last two: x[:N-2] Except first and last: x[1:N-1] Except first two: x[2:]

Python slice can be obtained by: x[starting_index:end_index] {including starting_index element and excluding end_index}

If you don't specify the starting_index, it becomes 0 by default. If you don't specify the end_index, it becomes N by default.

Related