Beginning Python from Novice to Professional (3)-list operation

Source: Internet
Author: User

List operations

List function:

[Python] View PlainCopy
    1. >>> list (' Hello ')
    2. [' h ', ' e ', ' l ', ' l ', ' o ']

Change the list:

[Python] View PlainCopy
    1. >>> x=[1,1,1]
    2. >>> x[1]=2
    3. >>> x
    4. [1, 2, 1]

To delete an element:

[Python] View PlainCopy
    1. >>> names = [' Wu ',' Li ', 'Zhao ',' Qian ']
    2. >>> del names[1]
    3. >>> names
    4. [' Wu ', ' Zhao ', ' Qian ']

Shard Assignment:

[Python] View PlainCopy
    1. >>> name = List (' perl ')
    2. >>> Name
    3. [' P ', ' e ', ' R ', ' l ']
    4. >>> name[2:] = list (' ar ')
    5. >>> Name
    6. [' P ', ' e ', ' a ', ' R ']
[Python] View PlainCopy
    1. >>> num = [1,2,3,4,5]
    2. >>> num[1:4]=[] # starts with 1 bits but does not include 4-bit
    3. >>> num
    4. [1, 5]

Add a new element at the end of the Append list:

[Python] View PlainCopy
    1. >>> LST = [1,2,3]
    2. >>> Lst.append (4)
    3. >>> LST
    4. [1, 2, 3, 4]

Number of Count statistics elements:

[Python] View PlainCopy
    1. >>> [' we ',' have ',' we ', 'a ',' dog '].count (' we ')
    2. 2

Extend extension list:

[Python] View PlainCopy
    1. >>> a = [1,2,3]
    2. >>> B = [4,5,6]
    3. >>> A.extend (b)
    4. >>> A
    5. [1, 2, 3, 4, 5, 6]

Index to find the location of the first occurrence:

[Python] View PlainCopy
    1. >>> sentence = [' I ',' have ',' a ',' little ',' dog ']
    2. >>> Sentence.index (' little ')
    3. 3

Insert inserts an object into the list:

[Python] View PlainCopy
    1. >>> num = [1,2,3,5,6,7]
    2. >>> Num.insert (3,' four ')
    3. >>> num
    4. [1, 2, 3, ' Four ', 5, 6, 7]

Pop removes the list element, the last one by default:

[Python] View PlainCopy
    1. >>> x = [1,2,3]
    2. >>> X.pop ()
    3. 3
    4. >>> x
    5. [1, 2]
    6. >>> X.pop (0)
    7. 1
    8. >>> x
    9. [2]

Combined Append:

[Python] View PlainCopy
    1. >>> x = [1,2,3]
    2. >>> X.append (X.pop ())
    3. >>> x
    4. [1, 2, 3]

Remove removes the first occurrence from the list:

[Python] View PlainCopy
    1. >>> x = [' to ',' is ', 'or ',' not ',' to ',' is ']
    2. >>> x.remove (' be ')
    3. >>> x
    4. [' to ', ' or ', ' no ', ' to ', ' being ']

Reverse Reverse storage elements:

[Python] View PlainCopy
    1. >>> x = [1,2,3]
    2. >>> X.reverse ()
    3. >>> x
    4. [3, 2, 1]

Sort order:

[Python] View PlainCopy
    1. >>> x = [4,6,2,1,7,9]
    2. >>> X.sort ()
    3. >>> x
    4. [1, 2, 4, 6, 7, 9]
[Python] View PlainCopy
      1. >>> X.sort (reverse=True)
      2. >>> x
      3. [9, 7, 6, 4, 2, 1]
      4. >>> X.sort (reverse=False)
      5. >>> x
      6. [1, 2, 4, 6, 7, 9]

Beginning Python from Novice to Professional (3)-list operation

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.