List and metadata related statements and methods in Python

Source: Internet
Author: User
This article mainly introduces the list and metadata related statements and methods in Python. It is the basic knowledge for getting started with Python. For more information, see List ):

First, the list is a sequence, so the sequence type can be used as the following built-in function --
List (iter): converts an iteratable object to a list.
Str (obj): converts an obj object to a string, which is represented by a string.
Tuple (iter): converts an iteratable object into a tuples.
Unicode (obj): converts an object to a Unicode string.
Basestring (): Abstract Factory functions only provide the parent class for str and unicode functions, so they cannot be instantiated or called.
Enumerate (iter): accepts an iteratable object as a parameter and returns an enumerate object, which generates a tuples consisting of the index value and item value of each element of iter.
Len (seq): return the length of seq.
Max (iter, key = None), max (arg0, arg1 ..., key = None): iter or (arg0, arg1. ..) is returned ...) if the 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): iter or (arg0, arg1. ..) is returned ...) if the key is specified, the key must be a callback function that can be passed to the sort () method for comparison.
Reversed (seq): receives a sequence as a parameter and returns an iterator that is accessed in reverse order.
Sorted (iter, cmp = None, key = None, reverse = False): accepts an iteratable object as a parameter and returns an ordered list, optional parameters include cmp, key, reverse, and list. the built-in functions of sort () have the same meaning.
Sum (seq, init = 0): returns the sum of seq and the optional parameter init, equivalent to reduce (operator. add, seq, init ).
Zip ([it0, it1. ..]): returns a list. The first element is it0, it1... A tuple composed of the first element of these elements, and other elements, and so on.

The list is like a linear container, but it is much larger than the lis t extension of C ++.
The elements in the list can be of the same type or contain various types, such as nesting another list in the list

List Example:

>>> L1 = [1,2,3] >>> type(L1) 
 
   >>> L1 = [1,'a',2,1.4] >>> L1 [1, 'a', 2, 1.4] >>> L1 = [ ['sub'],1,'n'] >>> L1 [['sub'], 1, 'n'] 
 

The index of list also starts from 0, but can also be accessed from the back. L1 [-1] indicates the last element in L1.

>>> L1 [['sub'], 1, 'n'] >>> L1[0] ['sub'] >>> L1[-1] 'n' 

You can slice the list. The Slice operation is similar to calling a function. The returned value is a new list.
Slice L1 [x: y: z] is a semi-open closed interval (z is usually not required ), for example, L1 [] returns a list from L1 [1] to L1 [2], excluding L1 [3].
If 'X' is not written, it indicates starting from the beginning, and 'y' indicates not written until the end of the list. 'z indicates the step size. The default value is 1, it can be considered that in this range, take one (take the first) for each z element, which can be a negative number, indicating traversing from the back to the front

>>> L1 = [1,2,3,4,5,6] >>> L1[1:3] [2, 3] >>> L1[:3] [1, 2, 3] >>> L1[1:] [2, 3, 4, 5, 6] >>> L1[-3:-1] [4, 5] >>> L2 = L1[:] >>> L2 [1, 2, 3, 4, 5, 6] >>> L1[::2] [1, 3, 5] >>> L1[::-1] [6, 5, 4, 3, 2, 1] 

The list can be used for addition, multiplication, and a string can also be seen as a list of characters.

>>> L1 = [1,2] >>> L2 = [3,4] >>> L1 + L2 [1, 2, 3, 4] >>> 5 * L1 [1, 2, 1, 2, 1, 2, 1, 2, 1, 2] 

In statement to determine whether an object is in a string/LIST/tuples
Not statement
Len can detect the number of elements in the string/LIST/ancestor/dictionary.
Max returns the maximum element, while min returns the minimum element.

>>> L1 [1, 2, 3, 4, 2] >>> 3 in L1 True >>> 5 in L1 False >>> 3 not in L1 False >>> 5 not in L1 True >>> len(L1) 5 >>> max(L1) 4 >>> min(L1) 1 

Operation:

>>># Assignment >>> L1 [1] = 5 >>> L1 [1, 5, 3, 4, 2] >>># Delete >>> del L1 [1] >>> L1 [1, 3, 4, 2] >>> # partition assignment >>> L1 [2:] = [6, 7, 8] >>> L1 [1, 3, 6, 7, 8]> L1 [] = []> L1 [1, 7, 8]

List functions:
Append (x) is to add x as an element to the end of the list, even if x is a list

>>> L1 [1, 2, 7, 8] >>> L1.append(3) >>> L1 [1, 2, 7, 8, 3] >>> L1.append([4,5]) >>> L1 [1, 2, 7, 8, 3, [4, 5]] >>> 4 in L1 False 


Count (x) counts the number of times x appears in the list

>>> L1 = [1, 2, 7, 8] >>> L1.count(2) 1 >>> L1.count(3) 0 


Extend (x) combines 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 the list and then perform the operation. If the operation fails, an error is returned.

>>> L1 [1, 2, 7, 8] >>> L1.extend([4,5]) >>> L1 [1, 2, 7, 8, 4, 5] >>> 4 in L1 True 


Index (x) returns the coordinates of x in the list. If x is not in the list, an error occurs.

>>> L1.index(2) 1 

Insert (I, x) insert element x in position I

>>> L1 [1, 2, 7, 8, 4, 5] >>> L1.insert(0,'a') >>> L1 ['a', 1, 2, 7, 8, 4, 5] >>> L1.insert(-1,'b') >>> L1 ['a', 1, 2, 7, 8, 4, 'b', 5] 

Pop (I) deletes the element at position I and returns it. By default, I can be left blank. If the last element is deleted, an error will occur if it does not exist.

>>> L1 = [1, 2, 7, 8] >>> L1.pop(1) 2 >>> L1 [1, 7, 8] >>> L1.pop() 8 >>> L1 [1, 7] 

Remove (x) removes the first match of x in the list. If x does not exist, an error occurs.

>>> L1.remove(2) >>> L1 [1, 7, 8] 


Reverse () returns the list in reverse order.

>>> L1 = [1, 2, 7, 8] >>> L1.reverse() >>> L1 [8, 7, 2, 1] 


Sort sorts the original list and returns None. There are two optional parameters: key and reverse. The default value is ascending.

>>> L1 [8, 7, 2, 1] >>> L1.sort() >>> L1 [1, 2, 7, 8] >>> L1.sort(reverse = True) >>> L1 [8, 7, 2, 1] >>> L1 = ['a','ccc','abcd','bc','cd','abc'] >>> L1.sort(key = len) >>> L1 ['a', 'bc', 'cd', 'ccc', 'abc', 'abcd'] 

Tuple)
Tuples also belong to the sequence, but the tuples are unchangeable lists. Therefore, there is no common method available for the above sequence of tuples!
The tuples of an element are expressed as (1 ,)

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

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

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

You can assign multiple values at a time using lists or tuples:

>>> L1 = (1,2,4) >>> (x, y, z) = L1 >>> x 1 >>> y 2 >>> z 4 >>> L1 = [1,2,4] >>> (x,y,z) = L1 >>> x 1 >>> y 2 >>> z 4 


[], And () indicate False in the Boolean Value

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.