This article mainly introduces the details of the differences between the Python list and numpy.ndarry slices, the list slice returned is not the original data, the changes to the new data will not affect the original data and numpy.ndarry the slice returned is the original data needs of friends can refer to the next
Details the difference between a Python list and a numpy.ndarry slice
Instance code:
# list slice returns the original data, and changes to the new data do not affect the original data in [the]: List1 = [1, 2, 3, 4, 5] in [the]: List2 = list1[:3]in [47]: LIST2OUT[47]: [1, 2, 3]in [in]: list2[1] = 1999# original data unchanged in []: list1out[50]: [1, 2, 3, 4, 5]in [Wuyi]: list2out[51]: [1, 19 99, 3]# and Numpy.ndarry The slice returned is the original data in []: arr = Np.array ([1, 2, 3, 4, 5]) in [the]: arrout[53]: Array ([1, 2, 3, 4, 5]) in [54 ]: arr1 = arr[:3]in [arr1out[55]: Array ([1, 2, 3]) in [2]: arr1[0] = 989In []: arr1out[57]: Array ([989, 3,]) # Repair Changed the original data in []: arrout[58]: Array ([989, 2, 3, 4, 5]) # If you want to get a copy of the original data, you can use copy () in [max]: arr2 = arr[:3].copy () in []: arr2 OUT[60]: Array ([989, 2, 3]) in []: arr2[1] = 99282In [+]: arr2out[62]: Array ([989, 99282, 3]) # The original data was not modified in []: Arro UT[63]: Array ([989, 2, 3, 4, 5])