Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Cortando en Rodajas | Indexing and Slicing
course content

Contenido del Curso

Ultimate NumPy

Cortando en RodajasCortando en Rodajas

Slicing en Python significa recuperar elementos desde un cierto índice a otro índice en una secuencia. En este capítulo, sin embargo, nos centraremos en el rebanado en matrices NumPy.

Rebanar en matrices 1D

La sintaxis general para cortar en arrays 1D es la siguiente: array[inicio:fin:paso]. Veámoslo con más detalle:

  • start es el índice a partir del cual se inicia el corte;
  • end es el índice en el que termina el corte (el índice en sí no se incluye);
  • step especifica los incrementos entre los índices (1 por defecto).

He aquí un ejemplo para aclararlo todo:

Slicing in 1D Arrays

The general syntax for slicing in 1D arrays is as follows: array[start:end:step]. Let’s take a more detailed look at it:

  • start is the index at which to start slicing.
  • end is the index at which slicing ends (the index itself is not included).
  • step specifies the increments between the indices (default is 1).

Here is an example to clarify everything:

1D slicing example

Here is the code for this example:

Let's take a look at a few more examples:

1D slicing example

Omitting Start, End, and Step

As you can see, we can often omit the start, end, step, or even all of them at the same time. For example, step can be omitted when we want it to be equal to 1. start and end can be omitted in the following scenarios:

  1. Omitting start:
    • Slicing from the first element (step is positive).
    • Slicing from the last element (step is negative).
  2. Omitting end:
    • Slicing to the last element inclusive (step is positive).
    • Slicing to the first element inclusive (step is negative).

Tarea

  1. Create a slice of array_1d with every second element starting from the second element to the end and store it in slice_1d (use a positive index for start and don't specify end).

¿Todo estuvo claro?

Sección 2. Capítulo 3
toggle bottom row
course content

Contenido del Curso

Ultimate NumPy

Cortando en RodajasCortando en Rodajas

Slicing en Python significa recuperar elementos desde un cierto índice a otro índice en una secuencia. En este capítulo, sin embargo, nos centraremos en el rebanado en matrices NumPy.

Rebanar en matrices 1D

La sintaxis general para cortar en arrays 1D es la siguiente: array[inicio:fin:paso]. Veámoslo con más detalle:

  • start es el índice a partir del cual se inicia el corte;
  • end es el índice en el que termina el corte (el índice en sí no se incluye);
  • step especifica los incrementos entre los índices (1 por defecto).

He aquí un ejemplo para aclararlo todo:

Slicing in 1D Arrays

The general syntax for slicing in 1D arrays is as follows: array[start:end:step]. Let’s take a more detailed look at it:

  • start is the index at which to start slicing.
  • end is the index at which slicing ends (the index itself is not included).
  • step specifies the increments between the indices (default is 1).

Here is an example to clarify everything:

1D slicing example

Here is the code for this example:

Let's take a look at a few more examples:

1D slicing example

Omitting Start, End, and Step

As you can see, we can often omit the start, end, step, or even all of them at the same time. For example, step can be omitted when we want it to be equal to 1. start and end can be omitted in the following scenarios:

  1. Omitting start:
    • Slicing from the first element (step is positive).
    • Slicing from the last element (step is negative).
  2. Omitting end:
    • Slicing to the last element inclusive (step is positive).
    • Slicing to the first element inclusive (step is negative).

Tarea

  1. Create a slice of array_1d with every second element starting from the second element to the end and store it in slice_1d (use a positive index for start and don't specify end).

¿Todo estuvo claro?

Sección 2. Capítulo 3
toggle bottom row
some-alt