[Python] the basis of a list of methods Summary

First, a list of

  Definition: for storing an ordered sequence data

  The syntax for declaring: [element 1, element 2, element 3, ...]

Second, characteristics

  1, is an ordered list of
  2, the list may store duplicate data
  3, the list can be stored in different data types
  4, is a list of variable data type; can be modified

Third, the method

  1, by

    (1) .append (obj) append
    (2) .insert (index, obj ) additional element in the specified location
    (3) .extend (iterable) iterables (sequence), the sequence of adding the elements in disposable end of the list
    (4) +
    (5) *

  2, delete

    (1) .pop (index) Deletes the specified index value of the element
    (2) .pop () default to delete the last element
    (3) .remove (obj) Removes the specified element
    (4) .clear () clear the list, - -> empty list

    (5) del python built-in function key is deleted
    (6) elements of a specified index value to delete the list del list [index] can be used to
    (7) del list can also be used to delete the entire list
        del str can to delete a string

  3, change

    (1) be changed by the index value -> list name [index] = new value
    (2) .reverse (): Reverse the list, a list of the elements reversed
    (3) .sort (key, reverse = False)
      Key: sorting rules
      reverse: the default is False positive sequence
      can be modified to True reverse

  4, check

    (1) .count number (obj) query elements appear in the list
    (2) .index (obj) query elements from the left for the first time appear in the list of index values, can not find error

Fourth, nested list

. 1  # the isinstance (object type) to determine whether the object is an element of type 
2 List2 = [[ ' A ' , ' B ' ], [ ' ABC ' , 100], " ABC " , 100, [0.9, 0.7 ] ]
 . 3  for X in List2:
 . 4      IF the isinstance (X, list):   # X is a listing: 
. 5          for Y in X:
 . 6              Print (Y)
 . 7      the else :
 . 8          Print (X)

Fifth, copy depth

  Copy List:
    1, the list of data stored in the memory,
      the entire list has a memory address, the memory address of each element in the list (of all understood as a variable), storing data corresponding to each variable

    2, shallow copy
      shallow copy of a list, with the inner nested list a copy of the object point to the same address;
      * saying goes: out by a shallow copy of a list, once the inner nested list is changed, and a copy of the object copy target, the inner nested list will change

    For shallow copy, the first layer is to create a new memory address, and the second layer from the beginning, is point to the same memory address, so for the second layer, and a deeper level, the consistency.

. 1  Import Copy # module .py file using functional blocks, the first step is to import the module into a working file 
2 copy.copy () # shallow copy

    3, deep copy  

. 1  Import Copy # module .py file using functional blocks, the first step is to import the module into a working file 
2 copy.deepcopy () # deep copy

    Nested variable data types reopened a memory
    copy of the object with the copy of the object, independent memory space, one changes, the other is not affected

Guess you like

Origin www.cnblogs.com/Tree0108/p/12109589.html