zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    cocos2dx[3.2](2) 3.x巨变
    cocos2dx[3.2](1) 浅析cocos2dx3.2引擎目录
    cocos2dx基础篇(28) 布景层Layer的三个子类
    cocos2dx基础篇(27) 屏幕适配
    centos 安装 mysql5.6
    centos 安装 mysql5.7.9初始密码问题
    php 计算字符串长度
    mysql tinyint
    php数组
    PHP中9大缓存技术总结
  • 原文地址:https://www.cnblogs.com/SouthRain/p/2254784.html
Copyright © 2011-2022 走看看