Python3 notes-list, python3 Notes List

Source: Internet
Author: User

Python3 notes-list, python3 Notes List

List deduplication methods:

1 # create a list and put data 2 a = [, 2] 3 B = [,] 4 5 d = [] 6 for I in: 7 if I not in d: 8 d. append (I) 9 print (d) 10 11 # set deduplication 12 B = set (B) 13 print (B)

List slice and flip list:

>>> S = 'abcdefgh' >>> s [:-1] # It can be regarded as a flip operation 'hgfedba' >>>> s [:: 2] # an operation to take an element 'aceg'

List sorting: sorted ()

You can retain the original list and get the sorted list sorted () as follows:

>>> a = [5,7,6,3,4,1,2]>>> b = sorted(a)>>> a[5, 7, 6, 3, 4, 1, 2]>>> b[1, 2, 3, 4, 5, 6, 7]

The sorted () method can be used in a sequence of any data type. The returned result is always in the form of a list:

>>> sorted('iplaypython.com')['.', 'a', 'c', 'h', 'i', 'l', 'm', 'n', 'o', 'o', 'p', 'p', 't', 'y', 'y']

 

Sequence type operators:

Seq [ind]: obtains the element whose subscript is ind.
Seq [ind1: ind2]: gets the element from ind1 to ind2, excluding the element of ind2.
Seq1 + seq2: concatenate sequence 1 and Sequence 2. This method is not the most effective. extend: seq1.extend (seq2)Print (seq1), if it is a string using. join
Obj in seq: determines whether the obj element is included in seq.
Boj not in seq: determines whether the obj element is not included in seq.

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.