Python Basics (List Class)

Source: Internet
Author: User

Lists (List Class)

Tip: All of the following methods are methods in the class, the first argument is self, and unity is not written.


The methods that are included are:

1, append (x) #将x添加到List末尾.

>>>list=[' a ']

>>>list.append (' B ')

>>>list

[' A ', ' B ']


2, Extend (L) #将表L附加到末尾, merge two lists.

>>>list=[' A ', ' B ']

>>>list2= [' C ', ' d ']

>>>list.extend (LIST2)

>>>list

[' A ', ' B ', ' C ', ' d ']


3, insert (i,x) #将元素x插入到 I this position.

>>>list.insert (0, ' test ')

[' Test ', ' a ', ' B ', ' C ', ' d ']


4, remove (x) #删除列表里第一个值为x的元素, if it does not exist, an error occurred

>>>list.remove (' test ')>>> cannot be a location

[' A ', ' B ', ' C ', ' d ']


5, Pop ([i]) #删除并返回位置为 element of I, if omitted, it means the last element stack is deleted: LIFO (LIFO)

>>> List.pop ()

' d '

>>> List

[' A ', ' B ', ' C ']

>>> List.pop (1)

' B '

>>> List

[' A ', ' C ']


6, index (i) #返回第一个值为x的元素的位置, if it does not exist, an error occurred

>>> List.index (' a ')

0

>>> list.index (' C ')

1


7, COUNT (x) #返回等于x的元素个数

>>> List

[' A ', ' C ', ' A ', ' a ', ' d ']

>>> list.count (' a ')

3


8, sort () #将List元素按照非递减排序, and a sort method is the sorted () function

>>> List.sort ()

>>> List

[' A ', ' a ', ' a ', ' C ', ' d ']

9, reverse () #将List元素反转排序

>>> List.reverse ()

>>> List

[' d ', ' C ', ' A ', ' a ', ' a ']


Next article

Dictionary (Dict Class)

This article from "A rookie on the Sky" blog, please be sure to keep this source http://rmeos.blog.51cto.com/761575/1710207

Python Basics (List Class)

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.