A list of Python learning

Source: Internet
Author: User

1.List function

The list function can create character lists through strings, as shown here:

>>> list (' Hello ') [' H ', ' e ', ' l ', ' l ', ' O ']



The list function can be used for all types of sequences, not limited to strings.

2. Basic operation of the list2.1 Element Assignment

>>> x=[1,2,3]>>> x[1]=4>>> x[1, 4, 3]

2.2 Deleting an element
>>> x[1, 4, 3]>>> del x[1]>>> x[1, 3]



2.3 Shard Assignment

>>> name=list (' Perl ') >>> name[' P ', ' e ', ' r ', ' L ']>>> name[2:][' r ', ' L ']>>> name[2: ]=list (' ar ') >>> name[' P ', ' e ', ' a ', ' R ']



>>> name[' P ', ' e ', ' a ', ' R ']>>> name[2:]=list (' Nsil ') >>> name[' P ', ' e ', ' n ', ' s ', ' I ', ' l ']> ;>> name[1:1]=[1,2,3]>>> name[' P ', 1, 2, 3, ' e ', ' n ', ' s ', ' I ', ' l ']>>> name[1:1]=list (' 123 ') > >> name[' P ', ' 1 ', ' 2 ', ' 3 ', 1, 2, 3, ' e ', ' n ', ' s ', ' I ', ' l ']>>> name[1:4]=[]>>> name[' P ', 1, 2, 3 , ' e ', ' n ', ' s ', ' I ', ' l ']



2.3append method

>>> lst=[1,2,3]>>> lst.append (4) >>> lst[1, 2, 3, 4]



2.4count method

>>> [' sfdf ', ' sfdf ', ' SD ', ' SD '].count (' SD ') 2>>> x=[[1,2],1,1,[2,1,[1,2]]]>>> x.count (1) 2



2.5extend Method
>>> a=[1,2,3]>>> b=[4,5,6]>>> a.extend (b) >>> a[1, 2, 3, 4, 5, 6]>>> b[4, 5, 6 ]>>> a+b[1, 2, 3, 4, 5, 6, 4, 5, 6]>>> a[1, 2, 3, 4, 5, 6]>>> a=a+b>>> a[1, 2, 3, 4, 5 , 6, 4, 5, 6]



2.6 Index Method

>>> knights=[' we ', ' is ', ' the ', ' knights ', ' who ', ' say ', ' ni ']>>> knights.index (' Who ') 4>>> Knights[4] ' Who '



2.7 Insert Method

>>> numbers=[1,2,3,4]>>> numbers.insert (0, ' AA ') >>> numbers[' AA ', 1, 2, 3, 4]



2.8pop method

>>> x=[1,2,3]>>> x.pop () 3>>> x[1, 2]>>> x.pop (0) 1>>> x[2]>>> x= [1,2,3]>>> X.append (X.pop ()) >>> x[1, 2, 3]>>> x.pop (0) 1>>> x.append (3) >> > x[2, 3, 3]



2.9remove method

>>> x=[' to ', ' being ', ' or ', ' not ', ' to ', ' being ']>>> x.remove (' be ') >>> x[' to ', ' or ', ' no ', ' to ', ' being ' ‘]



2.10 Reverse Method

>>> x=[1,2,3]>>> x.reverse () >>> x[3, 2, 1]



2.11 Sort Method

>>> x=[2,1,4,3,2,5]>>> x.sort () >>> x[1, 2, 2, 3, 4, 5]>>> x=[2,1,4,3,2,5]>> > y=x[:]>>> y.sort () >>> x[2, 1, 4, 3, 2, 5]>>> y[1, 2, 2, 3, 4, 5]>>> x=[4,6,2,1,7, 9]>>> y=sorted (x) >>> x[4, 6, 2, 1, 7, 9]>>> y[1, 2, 4, 6, 7, 9]

>>> x=[' qq ', ' dfsfd ', ' ssfd ', ' sdfdfffff ']>>> x.sort (key=len) >>> x[' qq ', ' ssfd ', ' dfsfd ', ' Sdfdfffff ']>>> x=[4,6,2,1,7,9]>>> x.sort (reverse=true) >>> x[9, 7, 6, 4, 2, 1]



all the attention points in the code, carefully experience the practice once again, Python's list operation is very powerful.

A list of Python learning

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.