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