Python Learning Note 4-list

Source: Internet
Author: User

List definition

>>> a=[' Free','1998']>>>type (a)<type'List'>>>> BOOL (a)#true if the list has a valueTrue>>>Printa[' Free','1998']>>>

Indexes and slices

 >>> a=[ " hello  , "  world  ,  '  2006   '   '  >>> a[1 '   '  world   '  >>> a[1:2< Span style= "color: #000000;" >][  " world    world   ' ] 

List function

>>>dir (list) ['__add__','__class__','__contains__','__delattr__','__delitem__','__delslice__','__doc__','__eq__','__format__','__ge__','__getattribute__','__getitem__','__getslice__','__gt__','__hash__','__iadd__','__imul__','__init__','__iter__','__le__','__len__','__lt__','__mul__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__reversed__','__rmul__','__setattr__','__setitem__','__setslice__','__sizeof__','__str__','__subclasshook__','Append','Count','Extend','Index','Insert','Pop','Remove','Reverse','Sort']

Appand Append element

>>> fruits=[‘Orange‘,‘apple "" banana ' ]>>> fruits.append ( '  ' banana ' 

extend merging of two lists

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

Extend object is a list, if it is str, then Python will first convert it by character to list and then append to the known list

>>> La = [a]>>> B ="ABC">>>La.extend (b)>>>la[1, 2, 3,'a','b','C']>>> C = 5>>>La.extend (c) Traceback (most recent): File"<stdin>", Line 1,inch<module>TypeError:'int'Object is  notIterable

Reverse reversal

Method 1[::-1]

>>> a=[1,2,3]>>> a[::-1[3, 2, 1>>> a="python  ">>> a[::-1]'nohtyp'

Method 2:list (Reversed (list))

>>> a=[1,2,3]>>> list (reversed (a)) [3, 2, 1]

Method 3:reverse

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

Count counts the number of occurrences of an element in a list and returns 0 if none

>>> c=[a,b,c,a,a]>>> c.count (a)3
>>> C.count (' F ')
0

Index to return indexes

>>> c=[a,b,c,a,a]>>> c.index (a)         #第一次出现的位置0

Insert

The I in List.insert (i, x) is the position where the element x is inserted into the list, and the x is inserted in front of the element where the index value is i. Note that the index is from 0

>>> l=['a','b','C','D']>>> L.insert (1,'#')>>>l['a','#','b','C','D']

If I is greater than the maximum value of the index, then it becomes an append

>>> a = [A.insert]>>> 9,777 >>> a[1, 2, 3, 777]

Remove

Remove the first matching element

>>> lst=['a','b','a','C']>>> Lst.remove ('a')>>>lst['b','a','C']

Pop

To remove an element from an index

>>> lst=['a','b','a','C']>>>lst.pop (0)'a'>>>lst['b','a','C']

Sort

By default, a small-to-large sort is implemented.

>>> lst=[4,5,7,1,9]>>> lst.sort ()>>> lst[1, 4, 5, 7, 9]

From big to small

>>> lst=[4,5,7,1,9]>>> lst.sort (reverse=True)>>> lst[9, 7, 5, 4, 1]

Sort by keyword

>>> LST = ["Python","Java","C","Pascal","Basic"]>>> Lst.sort (key=len)>>>lst['C','Java','Basic','Python','Pascal']

Python Learning Note 4-list

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.