Python Series tutorial (i) Python's "toil"--list

Source: Internet
Author: User

The list is very common in python and is very useful, so let's take a look at it today. First we need to remember two points: (1) The list is mutable (2) the list is suitable for all types of sequences, not just strings

Next we introduce the list of additions and deletions to change:

(1) Increase in list:

    1. Add a single element
    • Append method: This method is used to append a new object to the end of the list:
>>> list=[1,2,3,4,5,6]>>> list.append (7)>>> list[1, 2, 3, 4, 5, 6, 7]
    • Insert method: This method is used to insert an object into the list
>>> list=[1,2,3,4,5,6,7]>>> list.insert (3,'four') >>> list['four', 4, 5, 6, 7]

2. Add multiple elements

    • Extend method, this method can append multiple elements at the end
>>> a=[1,2,3,4,5]>>> b=[6,7,8,9,10]>>> a.extend (b) >>> a[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    • Using the connector "+"
>>> a=[1,2,3,4,5]>>> b=[6,7,8,9,10]>>> A +b[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    • Ways to use slices
>>> a=[1,2,3,4,5]>>> b=[6,7,8,9,10]>>> A[len (a):]=b>> > a[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Note: using "+" gets a new list instead of an extension to the original list.

(2) Deletion of the list:

    • del function: It can delete any element in the list
>>>a=[1, 2, 3, 4, 5, 6, 7, 8, 9, ten]del a[1]>>> a[1, 3, 4, 5, 6, 7, 8, 9, 10]
    • Pop function: Only the last element of the list can be deleted and the value of the element is returned
>>> a=[1, 3, 4, 5, 6, 7, 8, 9, ten]>>> a.pop ()10>>> a[1, 3, 4, 5, 6, 7, 8, 9]
    • Remove function: Delete matching elements
>>> a=[1, 3, 4, 5, 6, 7, 8, 9]>>> a.remove (7)>>> a[1, 3, 4, 5, 6, 8, 9]
    • Shard method to delete an element
>>> a=[1,2,3,4,5,6,7,8,9]>>> a[1:4]= []>>> a[1, 5, 6, 7, 8, 9 ]

(3) Modification of the list:

The modification of the list is very simple, mainly through the location index to modify

>>> a=[1,2,3,4,5,6,7,8,9]>>> a[1]=10>>> a[1, 10, 3, 4, 5, 6, 7, 8, 9]

(4) View of list: View by index location, Python support from back index, last position is-1

>>> a=[1,2,3,4,5,6,7,8,9]>>> a[2]3>>> a[3: [4, 5, 6, 7, 8, 9  ]>>> a[-1]9

Just introduced the list of additions and deletions, and then talk about the list of some other commonly used functions

(1) Method of sorting

There are two ways of sorting, one is through the sort function and the sorted function, so what is the difference between the two? The sort method is used to sort the list at the original location, which means that the original and list is changed. The sorted method is to return an already ordered list copy

>>> a=[4,6,2,3,9,8,1]>>> b=Sorted (a)>>> a[4, 6, 2, 3, 9, 8, 1  ]>>> b[1, 2, 3, 4, 6, 8, 9]>>> c=a>>> c.sort ()>& gt;> c[1, 2, 3, 4, 6, 8, 9]>>> a[1, 2, 3, 4, 6, 8, 9]

The Sort method also has two additional optional arguments,-key and reverse, with the keyword, creating a key for each element, and then all the elements are sorted by key. For example: Sort by the length of the element, you can make the Key=len.

>>> a=['de','s','HHT','Joiu']>>> A.sort (key=len)>>>a['s','de','HHT','Joiu']

Another parameter, reverse, is a simple Boolean value (Ture or false) to indicate whether to reverse-order

>>> a=[2,4,9,1,7,3,6,5]>>> a.sort (reverse=True)>>> a[9, 7, 6, 5, 4, 3, 2, 1]

(2) Reverse function

This function can store the elements of the list in reverse.

>>> a=[9, 7, 6, 5, 4, 3, 2, 1]>>> a.reverse ()>>> a[1, 2, 3, 4, 5, 6, 7, 9]

(3) The Count function

The function is to count the number of occurrences of an element in a list

>>> a=[1,2,3,4,5,1,2,6,7,8,9]>>> a.count (2)2

(4) Min and Max functions

They return the minimum and maximum values for the list, respectively

>>> a=[1, 2, 3, 4, 5,  6, 7, 8, 9]>>> min (a)1>>> Max (a)9

(5) Conversion between list and string

The list () function can convert other types to list types and can be converted to strings using the ". Join (Somelist).

>>> a=['a','b','C', ' D ' ]'. Join (a)'abcd'

This is a list of some basic operations, I hope to be able to help you.

Python Series tutorial (i) Python's "toil"--list

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.