python learning Notes (d) data containers - lists, tuples, dictionaries, collections overview

        Briefly summarize four data structures commonly used in python

First, a list of

  • Create a list
    Here Insert Picture Description

  • Additive element (append, extend, INSERT)
    Here Insert Picture Description
    append the same point that both extend the list by adding the tail element, and extend in a plurality of elements, when added, will not split; is directly and integrally append way to add
    addition, also by way of insert, the additive element to achieve the specified position
    Here Insert Picture Description

  • Remove elements (remove)
    Here Insert Picture Description

  • Clear List (clear)
    Here Insert Picture Description

  • Find an element (index)
    Here Insert Picture Description
    index lookup way: index (looking_for_value, start, end ), as long as the elements found satisfying the condition, i.e., returns the corresponding index; if not find, a ValueError error occurs
    generally end may be omitted, to the end of the list by default

    Small scale chopper: the existing 10 students, which is arbitrarily assigned to three classrooms in
    Here Insert Picture Description

Second, the tuple

  • Since no changes of tuples, so there is no tuple element adding and removing operation, conventional methods tuple index, count Here Insert Picture Description
    Note: When an internal list containing the tuple, can be modified list elements contained in the
    Here Insert Picture Description

Third, Dictionary

    python mapping of one type, such as a key-value pair in the form of key-value data storage

  • create
    Here Insert Picture Description

  • Add
    Method a: directly specify a key value which corresponds
    Here Insert Picture Description
    Method two: setDefault method, when the key-value pair is added, when the value is not specified, the default is None
    Here Insert Picture Description

  • Delete
    Here Insert Picture Description
    pop method of the list remove similar, but popitem list of pop is similar

  • Find
    Here Insert Picture Description
    dictionary [ 'key'] and dictionary.get ( 'key') can achieve the look, when the key is not present, the former will lead to error, which will return None

  • Traversal
    Here Insert Picture Description

Fourth, the collection

Is an unordered set of hash values ​​may be arranged, as a key dictionary.

  • Creating
    Here Insert Picture Description
    Note: When you create a non-empty set, the difference between the dictionary
  • Add to
    Here Insert Picture Description
  • delete
    Here Insert Picture Description
  • Intersection, and complement, the difference
    Here Insert Picture Description
    of which (blue) as shown in FIG complement crossing:
    Here Insert Picture Description
Published 20 original articles · won praise 3 · Views 1182

Guess you like

Origin blog.csdn.net/shine4869/article/details/104518384