0 Basic python-8.4 in situ Modify List Method summary

Source: Internet
Author: User

1. Indexes and slices, with indexes and slices, take the corresponding values out and replace them with new values

Here's an example of using a slice swap value


The order of execution of the above methods is: (For Alist[0:2]=alist[1:3])

1) Remove the value corresponding to Alist[1:3] first.

2) Delete the value of Alist[0:2]

3) Replace the Alist[1:3] on the alist[0:2]

2. Replace with a list method

1) use Append and +

Using append

Use +

The results are the same, and they're all changed.

As you can see from the code above, the ID always does not change, that is, Alist does not create new objects, all using the same object


3. Sorting

>>> alist=[' aba ', ' ABB ', ' abc ', ' Abd ']>>> alist.sort () >>> alist[' ABb ', ' Abd ', ' abc ', ' ABA '] >>> Alist.sort (Key=str.lower, reverse=true) >>> alist[' Abd ', ' ABC ', ' ABb ', ' AbA ']>>> Alist.sort (Key=str.upper  , reverse=true) >>> alist[' Abd ', ' ABC ', ' ABb ', ' AbA ']>>> alist.sort ( Key=str.upper  
Change the order of alist by changing the parameters in the sort

Note: Sorting is for objects of the same type, and different types of objects cannot be sorted

>>> alist=[' AbA ', ' ABb ', ' ABC ', ' Abd ',123]>>> alist.sort () Traceback (most recent call last):  File "<pyshell#50>", line 1, in <module>    

4. Some other methods




Right here, thank you.

------------------------------------------------------------------

Click to jump 0 basic python-Catalogue




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

0 Basic python-8.4 in situ Modify List Method summary

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.