There was a site that wrote notes on MATLAB and Python that I use at work, so I will keep it as a memorandum.
https://datachemeng.com/matlab_to_python/
import numpy as np
# =============================================================================
# b += 5 ->The original a also changes.
# =============================================================================
a = np.array([1,2,3])
print('a = ')
print(a)
print('\n')
b = a
b += 5 #The original a also changes.
print('b = a , b +=After running 5')
print('a = ')
print(a)
print('b = ')
print(b)
print('\n')
# =============================================================================
# 'b = a , b = b + 5 ->The original a does not change.
# =============================================================================
a = np.array([1,2,3])
b = a
b = b + 5 #The original a does not change.
print('b = a ,b = b +Run 5')
print('a = ')
print(a)
print('b = ')
print(b)
print('\n')
# =============================================================================
# 'b = a , b[0] = 100 ->The original a also changes.
# =============================================================================
a = np.array([1,2,3])
b = a
b[0] = 100
print('b = a ,b[0] =After running 100')
print('a = ')
print(a)
print('b = ')
print(b)
print('\n')
# =============================================================================
# b = a.copy() b[0] = 100 ->The original a also changes.
# =============================================================================
a = np.array([1,2,3])
b = a.copy()
b[0] = 100
print('b = a.copy(),b[0] =After running 100')
print('a = ')
print(a)
print('b = ')
print(b)
print('\n')
a =
[1 2 3]
b = a , b +=After running 5
a =
[6 7 8]
b =
[6 7 8]
b = a ,b = b +Run 5
a =
[1 2 3]
b =
[6 7 8]
b = a ,b[0] =After running 100
a =
[100 2 3]
b =
[100 2 3]
b = a.copy(),b[0] =After running 100
a =
[1 2 3]
b =
[100 2 3]
Recommended Posts