# Detail the comparison between Python list and ES2en.ndarry sections

• 2020-06-12 09:52:26
• OfStack

Details the differences between Python list and ES3en.ndarry slices

Example code:

``````
# list  The slice returns the original data, and the modification of the new data will not affect the original data
In [45]: list1 = [1, 2, 3, 4, 5]

In [46]: list2 = list1[:3]

In [47]: list2
Out[47]: [1, 2, 3]

In [49]: list2[1] = 1999

#  The original data didn't change
In [50]: list1
Out[50]: [1, 2, 3, 4, 5]

In [51]: list2
Out[51]: [1, 1999, 3]

#  while  NumPy.ndarry  The slice of is the original data
In [52]: arr = np.array([1, 2, 3, 4, 5])

In [53]: arr
Out[53]: array([1, 2, 3, 4, 5])

In [54]: arr1 = arr[:3]

In [55]: arr1
Out[55]: array([1, 2, 3])

In [56]: arr1[0] = 989

In [57]: arr1
Out[57]: array([989,  2,  3])

#  Modified the original data
In [58]: arr
Out[58]: array([989,  2,  3,  4,  5])

#  If you want a copy of the original data,   You can use  copy()
In [59]: arr2 = arr[:3].copy()

In [60]: arr2
Out[60]: array([989,  2,  3])

In [61]: arr2[1] = 99282

In [62]: arr2
Out[62]: array([ 989, 99282,   3])

#  The original data has not been modified
In [63]: arr
Out[63]: array([989,  2,  3,  4,  5])

``````