Contenido del Curso
Ultimate NumPy
2. Indexación y Segmentación
3. Funciones de NumPy comúnmente utilizadas
Ultimate NumPy
Cortando 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 is1
).
Here is an example to clarify everything:
Here is the code for this example:
Let's take a look at a few more examples:
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:
- Omitting
start
:- Slicing from the first element (
step
is positive). - Slicing from the last element (
step
is negative).
- Slicing from the first element (
- Omitting
end
:- Slicing to the last element inclusive (
step
is positive). - Slicing to the first element inclusive (
step
is negative).
- Slicing to the last element inclusive (
Tarea
- Create a slice of
array_1d
with every second element starting from the second element to the end and store it inslice_1d
(use a positive index forstart
and don't specifyend
).
¿Todo estuvo claro?
Contenido del Curso
Ultimate NumPy
2. Indexación y Segmentación
3. Funciones de NumPy comúnmente utilizadas
Ultimate NumPy
Cortando 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 is1
).
Here is an example to clarify everything:
Here is the code for this example:
Let's take a look at a few more examples:
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:
- Omitting
start
:- Slicing from the first element (
step
is positive). - Slicing from the last element (
step
is negative).
- Slicing from the first element (
- Omitting
end
:- Slicing to the last element inclusive (
step
is positive). - Slicing to the first element inclusive (
step
is negative).
- Slicing to the last element inclusive (
Tarea
- Create a slice of
array_1d
with every second element starting from the second element to the end and store it inslice_1d
(use a positive index forstart
and don't specifyend
).
¿Todo estuvo claro?