The Comparison Between Python list and NumPy. ndarry slice is described in detail,

Source: Internet
Author: User
Tags python list

The Comparison Between Python list and NumPy. ndarry slice is described in detail,

Differences between Python list and NumPy. ndarry slices

Instance code:

# List slices return non-original data. Modifications to new data will not affect the original data In [45]: list1 = [1, 2, 3, 4, 5] In [46]: list2 = list1 [: 3] In [47]: list2Out [47]: [1, 2, 3] In [49]: list2 [1] = 1999 # original data is not changed In [50]: list1Out [50]: [1, 2, 3, 4, 5] In [51]: list2Out [51]: [1, 1999, 3] # And NumPy. the ndarry slice returns the original data In [52]: arr = np. array ([1, 2, 3, 4, 5]) In [53]: arrOut [53]: array ([1, 2, 3, 4, 5]) in [54]: arr1 = arr [: 3] In [55]: arr1Out [55]: array ([1, 2, 3]) In [56]: arr1 [0] = 989In [57]: arr1Out [57]: array ([989, 2, 3]) # modify the original data In [58]: arrOut [58]: array ([989, 2, 3, 4, 5]) # to obtain a copy of the original data, you can use copy () In [59]: arr2 = arr [: 3]. copy () In [60]: arr2Out [60]: array ([989, 2, 3]) In [61]: arr2 [1] = 99282In [62]: arr2Out [62]: array ([989,992 82, 3]) # The original data is not modified In [63]: arrOut [63]: array ([989, 2, 3, 4, 5])

The above are Python list and NumPy. explanation of the differences between ndarry slices. If you have any questions, please leave a message or leave a message to the community on this site. Thank you for reading this article and hope it will help you. Thank you for your support for this site!

Related Article

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.