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

book
Copying Arrays

It's worth noting that NumPy offers various methods for duplicating arrays. In this section, we'll explore one of these methods, namely: .copy().

Note

The .copy() method creates a new array with the data from the original one.

Using the .copy() method:

import numpy as np

arr = np.array([7, 43, 56, 123, 10, 3])
x = arr.copy()

arr[0] = 42

print(arr)
print(x)
123456789
import numpy as np arr = np.array([7, 43, 56, 123, 10, 3]) x = arr.copy() arr[0] = 42 print(arr) print(x)
copy
Opgave

Swipe to start coding

You have the following array: [12, 56, 78, 65, 1, 5].

You have to obtain the following arrays using the correct method: arr_1 = [11, 56, 78, 0, 1, 5] arr_2 = [12, 56, 78, 65, 1, 5]

Replace element 12 with 11, and element 65 with 0.

Løsning

import numpy as np

# 1. Create an array
arr_1 = np.array([12, 56, 78, 65, 1, 5])
# 2. Copying the array
arr_2 = arr_1.copy()

# 3. Replacing the element 12 with 11, and the element 65 with 0
arr_1[0] = 11
arr_1[3] = 0

print(arr_1)
print(arr_2)

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 5
import numpy as np

# 1. Create an array
arr_1 = np.___([12, 56, 78, 65, 1, 5])
# 2. Copying the array
arr_2 = arr_1.___()

# 3. Replacing the element 12 with 11, and the element 65 with 0
arr_1[___] = ___
arr_1[___] = ___

print(arr_1)
print(arr_2)

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt