python built-in functions list (list)

First, a list of list

  •  A queue, a neat team, called the individual element within the list, the list consists of several elements, the element can be any object (numbers, strings, objects, lists, etc.)
  •  Sequential within the list of elements, the index may be used, a linear data structure, [], a list is variable
  •  Differences List list, list, queue, stack of

Second, define and initialize a list of list

  •  list () define an empty list list (iterable) to initialize an iterator list list can not define the size of the beginning  
  •  List definition mode: lst = list () or lst = []    
  • Example: lst = [2,6,9, 'ab'] lst = list (range (5))

Third, the access list index

  •  Index, also known as subscript
  •  Positive index: from left to right, starting from 0 in the list each element number
  •  Negative Index: right to left, starting from -1 to list each element number
  •  Positive and negative index can not be out of bounds, otherwise an exception is thrown IndexError, in order to facilitate understanding, can be considered a list arranged from left to right, the left is the head, the tail is on the right, the left is the lower bound, the right is the upper bound
  •  List through an index access methods: list [index], index is an index, the use of parentheses access


Fourth, the list query

1、list.index(value,[start,[stop])

  •  By value value, find elements within the list from the specified range matches, the first match will immediately return to the index, not match, an exception is thrown ValueError
  • For example:
  • >>> lst = [2,6,9,'ab']
    >>> lst.index(2)
    0
    >>> lst.index(6,0,3)
    1

2、list.count(value)

  •  The number of returns a list of matching value
  •  time complexity
  •  and index count methods are O (n)
  •  With the increase of the size of the list of data and reduced efficiency
  •  len (lst) returns a list of the number of elements
  • For example:
  • >>> lst = lst = [2,6,9,9,9,'ab']
    >>> lst
    [2, 6, 9, 9, 9, 'ab']
    >>> lst.count(9)
    3

Fifth, modify the list elements

  •  list [index] = value, not super-sector index

 

Sixth, the list increased, insert elements

  •  list.append (object): the tail of the list of additional elements, returns None, returns None means no new list generated in situ modification, the time complexity is O (1)  
  •  list.insert (index, object) return None, returns None means no new list generated in situ modification, the time complexity is O (1)  
  • Insert index out of bounds: Beyond the upper bound, the tail added, beyond the lower bound, the head added list.extend (iterable) return None, the iterable elements added in, return None, in situ modification  
  •  list + list: connecting operation connecting the two lists, a new list, change the original list, the call is essentially __add __ () method
  •  list * list: repeat, present a list of elements is repeated n times, and returns the new list

Seven, remove elements list

  •  list.remove (value): Returns None, find the first value matches the value from left to right, the element is removed, returns None, modified in situ
  •  list.pop ([index]): not specified index to pop an element from the end of the list, specify the index, the index from the pop-up element, the index of bounds error thrown IndexError
  •  list.clear (): Clears the list of all the elements, return None, remaining an empty list

Eight, a list of other actions

  •  list.reversed (): the list of elements reversed, return None, in situ modification
  •  list.sort (key = None, reverse = False): Sorts the elements of the list, in-place modify the default ascending order, reverse = True descending order, it is a key function, specify how the sort key
  •  whether in [3,4] in [1,2, [3,4]]

Nine, copy the list

1, the shadow copy, also called shallow copy: list.copy (), encounters a reference type, just copy a reference to it

  • Test lst1 = list (range (4)) and lst2 = list (range (4)) are equal?
  • >>> lst1 = list(range(4))
    >>> lst1
    [0, 1, 2, 3]
    >>> lst2 = list(range(4))
    >>> lst2
    [0, 1, 2, 3]
    >>> lst1 == lst2
    True
  • The above test description, lst1 and lst2 equal, the two lists point to the same reference
  • Test 1:
  • >>> lst1 = list(range(4))
    >>> lst2 = lst1.copy()
    >>> lst1
    [0, 1, 2, 3]
    >>> lst2
    [0, 1, 2, 3]
    >>> lst2[2] = 10
    >>> lst2
    [0, 1, 10, 3]
    >>> lst1
    [0, 1, 2, 3]
    >>> lst1 == lst2
    False

2, deep copy: copy module that provides deepcopy

  • Example 1: A shallow copy
  • >>> lst1 = [1,[2,3],4]
    >>> lst2 = lst1.copy()
    >>> lst2
    [1, [2, 3], 4]
    >>> lst2[1][1] = 20
    >>> lst2
    [1, [2, 20], 4]
    >>> lst1
    [1, [2, 20], 4]
    >>> lst1 == lst2
    True

     

    Example 2: using a deep copy
  • >>> lst1 = [1,[2,3],4]
    >>> lst1
    [1, [2, 3], 4]
    >>> import copy
    >>> lst2 = copy.deepcopy(lst1)
    >>> lst2
    [1, [2, 3], 4]
    >>> lst2[1][1] = 20
    >>> lst2
    [1, [2, 20], 4]
    >>> lst1 == lst2
    False

 

Guess you like

Origin www.cnblogs.com/jiangzuofenghua/p/11361384.html