2016.07.09-10 list, tuple

Source: Internet
Author: User

 list  initialization list: LST   =[] LST=list () LST= [1, 2, 3]   subscript/ Index Operations: indexes in Python starting with 0 Lst[0] take out the first element lst[-1] Negative index indicates from backward forward, starting from-1,-1 represents the last element if the index is out of range and throws an Indexerror exception when the element is modified, the indexerror exception increment elementis thrown if the index range is exceeded:Append method : Modify the list in situ, append an element to the end of the list, and the return value of the Append method is None        |Append (...) | L.append (object), None--Append object to end instance:>>> LST = [1, 2, 3]            >>> Lst.append (4)            >>>LST [1, 2, 3, 4]            >>>Insert  method : Inserts an element before the specified index position, the index of the insert operation is out of azimuth, and if it is a positive index, the equivalent of append is appended at the end of the list, if it is a negative index, equivalent to insert (0, object) |Insert (...) | L.insert (Index, object)--Insert Object before index instance:>>>Lst.insert (0,0)>>>LST [0,1, 2, 3, 4]            >>> Extend method : Extend an Iterative object to the list|Extend (...) | L.extend (iterable), None--extend list by appending elements fromThe iterable Example:>>>LST [78, 0, 1, 2, 10, 99, 3, 4, 100]            >>> L2 = [1,3,4,5,2]            >>>Lst.extend (L2)>>>LST LST>>>LST [78, 0, 1, 2, 10, 99, 3, 4, 100, 1, 3, 4, 5, 2]            >>> Delete element:(pop is for index, remove is for value) Pop method : index defaults to-1, delete-1 The element that is located, and returns it, can delete the element of the specified index, and throws a Indexerror exception if the specified index does not exist|Pop (...) | L.pop ([index]), item--Remove and returnitem at index (default last). | Raises indexerrorifList isEmptyorIndex isOut of range. Example:>>>LST [78, 0, 1, 2, 10, 99, 3, 4, 100, 1, 3, 4, 5, 2]            >>>Lst.pop ()2 >>> Lst.pop (4)            Ten >>> Lst.pop (100) Traceback (most recent): File"<stdin>", Line 1,inch<module>Indexerror:pop Index out of range>>> Remove method : Removes the first found element in the list and throws a ValueError exception if the value does not exist|Remove (...) | L.remove (value), None--Remove first occurrence of value. | Raises valueerrorifThe value is  notpresent. Example:>>>LST [78, 0, 1, 2, 99, 3, 4, 100, 1, 3, 4, 5]            >>> Lst.remove (1)            >>>LST [78, 0, 2, 99, 3, 4, 100, 1, 3, 4, 5]            >>> Lst.remove (7) Traceback (most recent): File"<stdin>", Line 1,inch<module>ValueError:list.remove (x): x not inchList>>> Clear Method , empty list|Clear (...) | L.clear (), None--Remove all items fromL Example:>>>LST [78, 0, 2, 99, 3, 4, 100, 1, 3, 4, 5]            >>>lst.clear ()>>>LST []>>>Find/statistical elements: Index method : Finds the index of the first occurrence of the specified element in the list, [Start,[stop]] is an optional parameter, and the value is an index that specifies the lookup range. |Index (...) | L.index (value, [Start, [stop]]), integer--returnFirst index of value. | Raises valueerrorifThe value is  notPresent instances:>>> LST = [1, 2, 3, 4, 5, 1, 3]            >>> Lst.index (1) 0>>> Lst.index (1,3)             5 >>> Count Method : Counts the number of occurrences of the specified element in the list. |count (...) | L.count (value), integer--returnNumber of occurrences of value instance:>>>LST [1, 2, 3, 4, 5, 1, 3]            >>> Lst.count (1)            2 >>> len function : is a built-in function that can be used to count the number of elements in a list. Len (obj,/) Return the number of itemsincha container. Example:>>>LST [1, 2, 3, 4, 5, 1, 3]            >>>Len (LST)7 >>> Modify list:sortMethod : Sorts the list, modifies the list in place, reverse as an optional parameter, defaults to False, specifies true, and sorts the list in reverse order.         |Sort (...) | L.sort (Key=none, Reverse=false), None--stable sort *in place*Example:>>>LST [1, 2, 3, 4, 5, 1, 3]            >>>Lst.sort ()>>>LST [1, 1, 2, 3, 3, 4, 5]            >>> Lst.sort (reverse=True)>>>LST [5, 4, 3, 3, 2, 1, 1] Reverse method : Reverses the list and modifies the list in place. |Reverse (...) | L.reverse ()--Reverse *in place*Example:>>> LST = [5, 4, 3, 3, 2, 1, 1]            >>>Lst.reverse ()>>>LST [1, 1, 2, 3, 3, 4, 5]            >>> Other methods:copymethod : Duplicate list, generate a new list, shallow copy.         |Copy (...) | L.copy () List--a shallow copy of L>>>LST [1, 1, 2, 3, 3, 4, 5]             >>> Lst2 =lst.copy ()>>>Lst2 [1, 1, 2, 3, 3, 4, 5]            >>>ID (LST)140291324960712 >>>ID (lst2)140291337551944 >>>In,not inmember Operator: Checks if the element is not in the list, returns a Boolean type>>>LST [0,1, 2, 3, 4, 5, 6, 7, 8, 9]            >>> 10inchLST False>>> 2inchLST True>>> List Slice assignment : If the content of the assignment is iterative, the original element is replaced (typically, no operation is assigned to the slice)>>> LST = list (range (0, 11))        >>>LST [0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10]        >>> Lst[3:5] = ['x','y','Z']        >>>LST [0,1, 2,'x','y','Z', 5, 6, 7, 8, 9, 10]        >>> >>> LST = list (range (0, 11))        >>> Lst[3:5] = ['x']        >>>LST [0,1, 2,'x', 5, 6, 7, 8, 9, 10]        >>>Tuple  (tuple): initialization tuple: t    = Tuple ([1, 2, 3]) T= (1, 2, 3) T= (1, the tuple is immutable, which is the biggest difference between tuples and lists.   Subscript/ Index Operations: use methods with tuples the same tuple method: Because tuples are immutable types, the tuple method only finds methods,count and index, with Same method as List

2016.07.09-10 list, tuple

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.