Acquaintance underlying data type list, tuple

List

List list variable (support indexing), ordered ( in situ modification )

You need to store large amounts of data and require these data when ordered.

An index list, and a slicing step size and index are not the same character string, the list can be modified by an index, as the string can not be changed, if the index is also given by the modified

  • increase

    append additional

    insert Insert insert (position, Inserts) position: this position until a new content

    extend iterative added

  • delete

    remove delete elements by name

    pop delete elements by index (default delete the last: "pop-up")

    clear Clear List

    del (del list name [index or slice or step])

  • change

    Modified by index

    By modifying sections: defaults in steps of 1, the item must be iterable, modify the content can be more or less

    Modified by step: step 1 is not necessary correspond

  • check

    for circulation and indexes, as well as a list of index names .index (element name) --- print display Index

  • Other methods:

    1. Find a list of index names .index (elements) by element
    2. Sort, the default is ascending list name .sort ()
    3. Descending list name .sort (reverse = True)
    4. Reverse, reverse the source data list name .reverse ()
    5. Artificial descending to ascending, then reversed
    6. Does not modify the source data is inverted lst1 = lst [:: - 1]

Tuple

Tuple tuple immutable, ordered tuple is an immutable list

Method tuple:

    - 统计  count(元素名称)
    - 获取索引 元组名.index(元素名称)
tu = (1,[]) * 3
print(tu)       # (1, [], 1, [], 1, [])
tu[-1].append(10)
print(tu)       # (1, [10], 1, [10], 1, [10])
#  列表在进行乘操作时,元素是共用的.元组同样如此

Guess you like

Origin www.cnblogs.com/lingshuai/p/11442922.html