• Python学习笔记之list各种方法


    definition:

    A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list. This is easy to imagine if you can think of a shopping list where you have a list of items to buy, except that you probably have each item on a separate line in your shopping list whereas in Python you put commas in between them.

    The list of items should be enclosed in square brackets so that Python understands that you are specifying a list. Once you have created a list, you can add, remove or search for items in the list. Since we can add and remove items, we say that a list is a mutable data type i.e. this type can be altered.

    有以下方法:append, count, extend, index, insert, pop, remove, reverse, sort

    1. List.append(object) 方法,追加对象到List中
    >>> L=[1,2,3]

    [1,2,3]

    >>> L.append(4)

    [1,2,3,4]

    2. List.extend(List) 方法,追加一个可迭代的对象到List中

    >>> L = [1,2,3]

    [1,2,3]

    >>> L.append([4])

    [1,2,3,4]

    >>> L.append([[4],[5]])

    [1,2,3,4,[4],[5]]

    *******通常合并2个List用extend,如果要把2个List合并到1个对象并保留2个List本身,直接用 List_C=List_A + List_B 的方式

    3.List.count(object) 方法,计算List中某个对象的出现次数

    >>> L= [1,2,3]

    [1,2,3]

    >>> L.count(2)

    1

    >>> L.append(2)

    [1,2,3,2]

    >>> L.count(2)

    2

    4.List.index(object) 方法,计算List中某个对象第一次出现的位置

    >>> L= [1,2,3,3]

    [1,2,3,3]

    >>> L.index(3)

    2

    5.List.insert(index,object) 方法,在指定位置增加一个元素

    >>> L= [1,2,3]

    [1,2,3]
    >>> L.insert(0,10)

    [10,1,2,3]

    6.List.pop(index)方法,取出指定位置的元素

    >>> L= [1,2,3]

    [1,2,3]
    >>> L.pop(0)

    1

    >>> L

    [2,3]

    7.List.remove(object)方法,移除第一个匹配的指定对象

    >>> L= [1,2,3,3,4]

    [1,2,3,3,4]

    >>> L.remove(3)

    >>> L

    [1,2,3,4]

    8.List.sort() 方法,顺序排序

    >>> L= [1,2,3,5,4]

    [1,2,3,4,5]

    9.List.reverse() 方法,逆序排序

    >>> L= [1,2,3,3,4]

    [1,2,3,3,4]

    >>> L.reverse()

    [4,3,3,2,1]

    List通用可以用for ... in ...来迭代读取

    >>> List = [1,2,3,4]

    [1,2,3,4]

    >>> for L in List:

    ... print L

    ...

    1

    2

    3

    4

    迭代后操作

    >>> square = [L **2 for L in List]

    >>> square

    [1,4,9,16]

    其实就等于

    >>> square = []

    >>> for L in List:

    ... square.append(L**2)

    ...

    >>> square

    [1,4,9,16]

    基本方法:

    List * i 重复一个list

    >>> List = [1,2]

    [1,2]

    >>> List * 2

    [1,2,1,2]

    List[i:j] 分片

    >>> List = [1,2,3,4]

    [1,2,3,4]

    >>> List[1:2]

    [2]

    >>> List[1:3]

    [2,3]

    object in List 判断存在

    >>> List = [1,2,3,4]

    [1,2,3,4]
    >>> 1 in List

    True

    >>> 5 in List

    False

    del List[index] 删除对象

    >>> List = [1,2,3,4]

    [1,2,3,4]
    >>> del List[0]

    [2,3,4]

    del List[i:j] 删除片

    >>> List = [1,2,3,4]

    [1,2,3,4]
    >>> del List[0:2]

    [3,4]

    range(digital) 生成整数列表/元组

    >>> List = range(4)

    [0,1,2,3]

    xrange 与range作用大致相同,但是生成方法不一样,性能更好,如果需要循环的列表比较大,建议用xrange
  • 相关阅读:
    【刷题】UOJ #274 【清华集训2016】温暖会指引我们前行
    【刷题】BZOJ 3669 [Noi2014]魔法森林
    【刷题】BZOJ 2594 [Wc2006]水管局长数据加强版
    (84)Wangdao.com第十八天_JavaScript 文档对象模型 DOM
    (84)Wangdao.com第十八天_JavaScript Promise 对象
    (83)Wangdao.com第十七天_JavaScript 定时器
    (82)Wangdao.com第十六天_JavaScript 异步操作
    (81)Wangdao.com第十六天_JavaScript 严格模式
    (80)Wangdao.com第十六天_JavaScript Object 对象的相关方法
    (79)Wangdao.com第十五天_JavaScript 对象的继承_prototype原型对象_封装_函数式编程
  • 原文地址:https://www.cnblogs.com/SouthRain/p/2254784.html
Copyright © 2020-2023  润新知