A detailed comparison of the list and numpy.ndarry slices in python

Source: Internet
Author: User
Tags python list
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]) 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.