Explanations of the related statements and methods of lists and tuples in Python

Source: Internet
Author: User
List:

First, the list belongs to the sequence, then the sequence type can be built as follows--
List (ITER): Converts an Iterative object to a list.
STR (obj): Converts the Obj object to a string, that is, the object is represented by a string.
Tuple (ITER): Converts an iterative object to a tuple.
Unicode (obj): Converts an object to a Unicode string.
Basestring (): Abstract factory function that simply provides the parent class for STR and Unicode functions, so it cannot be instantiated or invoked.
Enumerate (ITER): Accepts an iterative object as a parameter that returns a enumerate object that generates a tuple of index and item values for each element of ITER.
Len (seq): Returns the length of the seq.
Max (Iter,key=none), Max (Arg0,arg1...,key=none): Return to ITER or (Arg0,arg1 ...) Maximum value, if key is specified, the key must be a callback function that can be passed to the sort () method for comparison.
Min (iter,key=none), Min (arg0,arg1...,key=none): Return to ITER or (Arg0,arg1 ...) Minimum value, if key is specified, the key must be a callback function that can be passed to the sort () method for comparison.
Reversed (seq): Takes a sequence as a parameter, returning an iterator that is accessed in reverse order.
Sorted (Iter,cmp=none,key=none,reverse=false): Accepts an iterative object as a parameter, returning an ordered list, with optional parameters like CMP, key and reverse and list.sort () built-in functions.
SUM (seq,init=0): Returns the sum of the SEQ and optional parameter init, which has the same effect as reduce (operator.add,seq,init).
Zip ([it0,it1 ...]) : Returns a list whose first element is It0, it1 ... The first element of these elements consists of a tuple, and the other elements in turn.

The list is like a linear container, but much more than the C + + lis t extension
The elements in the list can be of the same type, or they can contain various types, such as a list nested in another table

Example of a column:

>>> L1 = [2] >>> type (L1) 
 
  
   
   >>> L1 = [1, ' A ', 2,1.4] >>> L1 [1, ' A ',, 1 .4] >>> L1 = [[' Sub '],1, ' n '] >>> L1 [[' Sub '], 1, ' n '] 
 
  

The index of the list is also starting from 0, but can also be accessed from after, L1[-1] represents the last element in L1

The list can be sliced, and the slice operation is similar to a call to a function that returns a new list of values
Slice l1[x:y: z] is a semi-open interval (z is usually not written), such as L1[1:3] returns a list from the end of l1[1] to l1[2, and does not contain l1[3]
X does not write to start from scratch, y does not write until the end of the list, z is used to represent the step, the default is 1, can be considered in this interval each z element takes one (take the first), can be negative, indicating from the back to the front traversal

Lists can do addition, multiply, and strings can also be seen as a list of characters

In statement to determine whether an object is in a string/list/tuple
The not statement represents the negation of the following
Len can detect the number of elements in a string/list/meta-ancestor/dictionary
Max can return the largest element, Min returns the smallest element

Operation:

Function of list:
Append (x) is the addition of x as an element to the end of the list, even if X is a list


COUNT (x) counts the number of occurrences of x in the list


Extend (x) merges x as a list with the original list and adds it to the end. If it is not a list, the compiler tries to convert X to a list and then executes the action, and the error will be incorrect.


Index (x) returns the coordinates of x in the list, if X is not in the list error

Insert (i, x) inserts element x in position I

Pop (i) delete the element of position I and return it, default can not write I, delete the last element, there will be an error

Remove (x) removes the first occurrence of x in the list, and X does not exist error


Reverse () Reverse the list


Sort sorts the original list, returns none, has two optional arguments, key and reverse, and defaults to ascending order

Tuple (tuple)
Tuples are also part of a sequence, but tuples are not modifiable lists. Therefore the tuple does not have the above sequence common method to be available!
A tuple of an element is represented as (1,)

>>> x = (1,) >>> type (x) 
 
  
   
   >>> x = (1) >>> type (x) 
  
    
  
   
 
  

Tuples can be converted to lists, and vice versa.
The built-in tuple () function takes a list parameter and returns a tuple containing the same element, while the list () function takes a tuple parameter and returns a list.
From the effect, the tuple () freezes the list, and the list () melts the tuple.

>>> x = [1,2,4,3,1] >>> y = (1,2,4,3,1) >>> type (x) 
 
  
   
   >>> type (y) 
  c11>
    >>> z = tuple (x) >>> Z (1, 2, 4, 3, 1) >>> z = List (y) >>> Z [1, 2, 4, 3, 1] 
  
 
  

You can use a list or tuple to make multiple assignments at once:


[], and () represent False in a Boolean value

  • 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.