Conteúdo do Curso
Ultimate NumPy
3. Commonly used NumPy Functions
Ultimate NumPy
Slicing
Slicing in Python refers to retrieving elements from one index to another within a sequence. In this chapter, however, we will focus on slicing in NumPy arrays.
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 (purple squares represent the elements retrieved from slicing):
![1D slicing example](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/4f4826d5-e2f8-4ffd-9fd0-6f513353d70a/section_2/1d_slicing_1.png)
Note
Since we did not explicitly specify
step
, it defaults to a value of1
.
Here is the code for this 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:
- 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 (
Let's take a look at a few more examples (the black arrow indicates that the elements are taken in reverse order):
![1D slicing example](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/4f4826d5-e2f8-4ffd-9fd0-6f513353d70a/section_2/1d_slicing_2.png)
Here is the corresponding code:
Tarefa
You are analyzing the daily sales data of a small retail store. The sales for the past week are stored in a NumPy array, with each element representing the sales for a specific day. Create a slice of weekly_sales
containing the sales data for every second day, starting from the second day (Tuesday), and store it in alternate_day_sales
(use a positive index for start
and do not specify end
).
Tudo estava claro?
Conteúdo do Curso
Ultimate NumPy
3. Commonly used NumPy Functions
Ultimate NumPy
Slicing
Slicing in Python refers to retrieving elements from one index to another within a sequence. In this chapter, however, we will focus on slicing in NumPy arrays.
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 (purple squares represent the elements retrieved from slicing):
![1D slicing example](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/4f4826d5-e2f8-4ffd-9fd0-6f513353d70a/section_2/1d_slicing_1.png)
Note
Since we did not explicitly specify
step
, it defaults to a value of1
.
Here is the code for this 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:
- 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 (
Let's take a look at a few more examples (the black arrow indicates that the elements are taken in reverse order):
![1D slicing example](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/4f4826d5-e2f8-4ffd-9fd0-6f513353d70a/section_2/1d_slicing_2.png)
Here is the corresponding code:
Tarefa
You are analyzing the daily sales data of a small retail store. The sales for the past week are stored in a NumPy array, with each element representing the sales for a specific day. Create a slice of weekly_sales
containing the sales data for every second day, starting from the second day (Tuesday), and store it in alternate_day_sales
(use a positive index for start
and do not specify end
).
Tudo estava claro?