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
  • 相关阅读:
    【整理】close 和 shutdown 的原理
    【理解】 Error 10053和 Error 10054
    【转载】 socket recv 和 read
    【转载】socket 的 connect、listen、accept 和全连接队列、半连接队列的原理
    【原创】MySQL 生产环境备份还原
    【原创】【问题记录】系统管理员设置了系统策略,禁止此安装的最终解决办法
    【原创】rabbitmq 学习
    mvc, web mvc, spring web mvc 区别
    Spring 读取配置文件的俩种方式
    移动端web开发技巧和常见问题
  • 原文地址:https://www.cnblogs.com/SouthRain/p/2254784.html
Copyright © 2011-2022 走看看