Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Sorting Arrays | Important Functions
NumPy in a Nutshell

book
Sorting Arrays

Now, let's dive into sorting arrays. Sorting an array involves arranging its elements in a specific order, whether it's ascending, descending, or some other sequence.

Let's try to sort the array:

import numpy as np

array = np.array([3, -4, 6, 0, 12, 499, -123])

print('unsorted array', array)
print('sorted array', np.sort(array))
123456
import numpy as np array = np.array([3, -4, 6, 0, 12, 499, -123]) print('unsorted array', array) print('sorted array', np.sort(array))
copy

Note

The sort() function returns a copy of the array, while the original array remains unchanged.

Let's try to sort a 2-D array:

import numpy as np

array = np.array([[-2, 4, -12, -434, 62], [1, 4, 7, 93, 75]])
print('Unsorted array', array)
print('Sorted array', np.sort(array))
12345
import numpy as np array = np.array([[-2, 4, -12, -434, 62], [1, 4, 7, 93, 75]]) print('Unsorted array', array) print('Sorted array', np.sort(array))
copy
Uppgift

Swipe to start coding

You have the following array: [15, -2, 33, 47, -55, 16, 0, 834]. Your task is to sort it.

Lösning

import numpy as np

# 1. Creating an array
arr_1 = np.array([15, -2, 33, 47, -55, 16, 0, 834])

# 2. Sorting the array
print('Sorted array_1:', np.sort(arr_1))

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 4
import numpy as np

# 1. Creating an array
arr_1 = np.___([15, -2, 33, 47, -55, 16, 0, 834])

# 2. Sorting the array
print('Sorted array_1:', np.___(___))

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt