List of Python

Source: Internet
Author: User
Tags iterable shallow copy

A list is also a serialized data type that can be accessed by subscript or slice operations to access a contiguous element or segment.

First, create a list

A list can be defined by square brackets ' [] ' and created by assigning variables.

>>> L1 = ['abc','456','xyz'  ]>>> l1['ABC'456   'xyz']

You can nest a list in a list, like this:

>>> L2 = ['ABC','456','XYZ',['789','LMN']]>>>l2['ABC','456','XYZ', ['789','LMN']]

Second, the operation of the list

A. Access

Use the slice operation and index values to access the list.

>>>l2['ABC','456','XYZ', ['789','LMN']]>>>L2[0]'ABC'>>> l2[-1]['789','LMN']>>> l2[3][1]'LMN'
Index

B. Updates

Use the Append () method to append the element to the list.

>>> L1 = ['ABC','456','XYZ']>>> L1.append ('Li')>>>l1['ABC','456','XYZ','Li']
Append

Three, several useful functions

1. Len ()

Number of elements in the statistics list

>>> l3['2'3'5'  '6'9']>>> Len ( L3)5
Len

2. Max () &min ()

Gets the maximum and minimum values in the list

>>> Max (L3)'9'>>> min (L3)'2  '
Max&min

3. Enumerate ()

Iterate over the elements in the sequence and their subscripts

 for  in Enumerate ([+]):print11 22 3
Enumerate

4. Sum

Add the values in the list

>>> L4 = [Int ('2'), int ('4')]>>> sum (L4)6
sum

Note: only integers and string types can be added, otherwise an error occurs

Iv. built-in functions

    defAppend (self, p_object):#add an object to the list    defClear (self):#Clear List    defcopy (self):#Shallow Copy    defcount (self, value):#Count the number of occurrences of an object in a list    defExtend (self, iterable):#add content to the list>>>L1 ['ABC','456','XYZ','Li']    >>>L1 ['ABC','456','XYZ','Li']    >>>L2 ['ABC','456','XYZ', ['789','LMN']]    >>>L1.extend (L2)>>>L1 ['ABC','456','XYZ','Li','ABC','456','XYZ', ['789','LMN']]    defIndex (self, value, Start=none, stop=None):#gets the index position in the list        """def insert (self, Index, p_object): #指定下标插入元素 def pops (self, index=none): #指定下标删除元素, and gets the delete element (if No object specified, default is last value) def remove (self, value): #指定值删除的元素 def reverse (self): #反向列表中元素 def sort (self, key=none, R    Everse=false): #对列表中的元素进行排序 >>> l3 = [' 3 ', ' 6 ', ' 5 ', ' 9 ', ' 2 '] >>> l3.sort () >>> L3 [' 2 ', ' 3 ', ' 5 ', ' 6 ', ' 9 '] def __add__ (self, *args, **kwargs): # Real Signature Unknown"""Return Self+value."""Pass def __contains__ (self, *args, **kwargs): # Real Signature Unknown"""Return KeyinchSelf."""Pass def __delitem__ (self, *args, **kwargs): # Real Signature Unknown"""Delete Self[key]."""Pass def __eq__ (self, *args, **kwargs): # Real Signature Unknown"""Return Self==value."""Pass def __getattribute__ (self, *args, **kwargs): # Real Signature Unknown"""Return getattr (self, name)."""Pass def __getitem__ (self, y): # real signature unknown; restored from __doc__"""X.__getitem__(y) <==> X[y]"""Pass def __ge__ (self, *args, **kwargs): # Real Signature Unknown"""Return Self>=value."""Pass def __gt__ (self, *args, **kwargs): # Real Signature Unknown"""Return Self>value."""Pass def __iadd__ (self, *args, **kwargs): # Real Signature Unknown"""Implement Self+=value."""Pass def __imul__ (self, *args, **kwargs): # Real Signature Unknown"""Implement Self*=value."""Pass def __init__ (self, seq= ()): # Known special case of list.__init__"""list ()-New empty list List (iterable)New list initialized fromIterable's Items        #(copied from class Doc)        """Pass def __iter__ (self, *args, **kwargs): # Real Signature Unknown"""Implement iter (self)."""Pass def __len__ (self, *args, **kwargs): # Real Signature Unknown"""Return len (self)."""Pass def __le__ (self, *args, **kwargs): # Real Signature Unknown"""Return Self<=value."""Pass def __lt__ (self, *args, **kwargs): # Real Signature Unknown"""Return Self<value."""Pass def __mul__ (self, *args, **kwargs): # Real Signature Unknown"""Return SELF*VALUE.N"""Pass @staticmethod # known case of __new__ def __new__ (*args, **kwargs): # Real Signature Unknown """Create and returnA new object. See Help (Type) forAccurate signature."""Pass def __ne__ (self, *args, **kwargs): # Real Signature Unknown"""Return Self!=value."""Pass def __repr__ (self, *args, **kwargs): # Real Signature Unknown"""Return repr (self)."""Pass def __reversed__ (self): # Real Signature unknown, restored from __doc__"""L.__reversed__() --returnA reverse iterator over the list"""Pass def __rmul__ (self, *args, **kwargs): # Real Signature Unknown"""Return Self*value."""Pass def __setitem__ (self, *args, **kwargs): # Real Signature Unknown"""Set Self[key] to value."""Pass def __sizeof__ (self): # Real Signature unknown, restored from __doc__"""L.__sizeof__()--size of LinchMemoryinchbytes"""Pass __hash__ = None
method&f (x)

List of Python

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.