• 1.2数据类型(list)


    列表创建

    创建一个列表,只要用逗号分隔不同的数据项使用方括号括起来即可,例如:

    first_list = ["hello","python",1900,1970]

    列表访问

    使用下标索引访问列表中的值,也可通过方括号进行截取。例如:

    >>> first_list = ["hello","python",1900,1970]
    >>> first_list[0]
    'hello'
    >>> first_list[1:3]
    ['python', 1900]

    列表更新

    可以索引对列表的数据项进行修改或更新,也可用append方法来增加列表项,例如

    >>> first_list = ["hello","python",1900,1970]
    >>> first_list[1] = "yangyang"
    >>> first_list
    ['hello', 'yangyang', 1900, 1970]

    append例子,例如:
    >>> first_list = ["hello","python",1900,1970]

    >>> first_list.append(2017)

    >>> first_list
    ['hello', 'yangyang', 1900, 1970, 2017]

    列表元素删除

    使用del删除列表中的元素,例如:

    >>> first_list = ["hello","python",1900,1970]
    >>> del first_list[3]
    >>> first_list
    ['hello', 'python', 1900]

    列表截取

    >>> first_list = ["hello","python",1900,1970]
    >>> first_list[1]
    'python'
    >>> first_list[-2]
    1900
    >>> first_list[1:3]
    ['python', 1900]
    >>> first_list[1:]
    ['python', 1900, 1970]
    >>> first_list[-1:-3:-1]
    [1970, 1900]

    列表运算符:+、*、[]、[:]、in、not in、for i in list

     a = ["hello","python"],b = ["hello","yangyang"]

    操作符 描述  实例 
     +  组合  

    >>> a = ["hello","python"]
    >>> b = ["hello","yangyang"]
    >>> a + b
    ['hello', 'python', 'hello', 'yangyang']

     重复  

    >>> a * 4
    ['hello', 'python', 'hello', 'python', 'hello', 'python', 'hello', 'python']

    [] 索引

    >>> a[0]
    'hello'

    [:] 截取

    >>> a[0:2]
    ['hello', 'python']

     in  成员运算符,元素在列表中返回True  

    >>> "hello" in a
    True

     not in  成员运算符,元素不在列表中返回True  

    >>> "world" not in a
    True

     for i in list  迭代  

    >>> for i in a:
    print i

    hello
    python

     

    列表函数

    语法
    描述 参数 返回值 实例
    cmp(list1,list2) 比较两个列表的元素

    list1--比较列表

    list2--比较列表

    list1 > list2:1

    list1 < list2:-1

    list1 == list2:0

    >>> list2 = [456,"abc"]
    >>> list1 = [123,"xyz"]
    >>> print cmp(list1,list2)
    -1
    >>> print cmp(list2,list1)
    1
    >>> list3 = list2 +[789]
    >>> print cmp(list2,list3)
    -1

    len(list) 列表元素个数 list--要计算元素个数的列表 返回元素个数

    >>> list1 = [1,2,3,4,5,6]
    >>> len(list1)
    6

    max(list) 返回列表元素最大值 list--要返回最大值的列表 返回列表中的最大值

    >>> list1 = [1,2,3,4,5,6]
    >>> max(list1)
    6

    min(list) 返回列表元素最小值 list--要返回最小值的列表 返回列表中的最小值

    >>> list1 = [1,2,3,4,5,6]
    >>> min(list1)
    1

    list(seq) 将String和tuple转换成列表 seq--要转换成列表的string或tuple 返回列表

    >>> tuple1 = (1,2,3,4,5)
    >>> tuple_list1 = list(tuple1)
    >>> tuple_list1
    [1, 2, 3, 4, 5]
    >>> string1 = "abcdefg"
    >>> str_list = list(string1)
    >>> str_list
    ['a', 'b', 'c', 'd', 'e', 'f', 'g']

    列表方法

    语法 描述 参数 返回值 实例
    list.append(obj) 列表末尾添加新对象 obj--添加到列表末尾的对象 无返回值,修改原来列表

    >>> list1 = [1,2,3,4]
    >>> list1.append("hello")
    >>> list1
    [1, 2, 3, 4, 'hello']

    list.count(obj) 统计某个元素在列表中出现的次数 obj--列表中的统计对象 元素在列表中出现的次数

    >>> list1 = [1,2,3,4,1,2,3]
    >>> list1.count(1)
    2

    list.extend(seq) 列表末尾追加另一个列表的多个值(用新列表扩展原来列表) seq--扩展列表 无返回值,修改原有列表

    >>> list1 = [1,2,3,4,5]
    >>> list2 = ["hello","python"]
    >>> list1.extend(list2)
    >>> list1
    [1, 2, 3, 4, 5, 'hello', 'python']

    list.index(obj) 列表中找出某个值第一匹配的索引位置 obj--查找的对象 返回查找对象的索引位置,无抛出异常

    >>> list1 = [1,2,3,1,4]
    >>> list1.index(2)
    1
    >>> list1.index(5)

    Traceback (most recent call last):
    File "<pyshell#30>", line 1, in <module>
    list1.index(5)
    ValueError: 5 is not in list

    list.insert(index,obj) 将对象插入列表的指定位置

    index--插入的索引位置

    obj--要插入列表中的对象

    无返回值,会在原有列表中插入对象

    >>> list1 = [1,2,3,4,5]
    >>> list1.insert(2,7)
    >>> list1
    [1, 2, 7, 3, 4, 5]

    list.pop([obj]) 移除某个元素,并返回该元素的值 obj--可选参数,要移除列表的元素的index 返回移除元素对象

    >>> list1 = [1,2,1,3,4,5]
    >>> list1.pop()
    5
    >>> list1.pop(1)
    2
    >>> list1
    [1, 1, 3, 4]
    >>> list1.pop(2)
    3

    list.remove(obj) 移除列表中摸个值的第一个匹配项 obj--列表中要移除的对象 无返回,移除列表中对象

    >>> list1 = [1,2,1,3,4,5]
    >>> list1.remove(1)
    >>> list1
    [2, 1, 3, 4, 5]

    list.revese() 反向列表元素 无,对列表元素反向排序

    >>> list1 = [1,2,1,3,4,5]
    >>> list1.reverse()
    >>> list1
    [5, 4, 3, 1, 2, 1]

    list.sort([func]) 对列表元素进行排序 func--可选参数,若指定func则用func方法进行排序 无返回值,对列表对象进行排序  

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

    >>> list1.sort()
    >>> list1
    [1, 1, 2, 3, 4, 5]

     列表解析

    列表解析表达式1:[expression for target1 in iterable1 [if condition1],例子:

    >>> [i *2 for i in range(1,10) if i % 2 == 0]
    [4, 8, 12, 16]

    列表解析表达式2--加入嵌套循环:[expression for target1 in iterable1 [if condition1]...

                          for targetn in iterablen [if conditionn] ],例子:

    >>> [(i*i,j*j) for i in range(1,5) if i % 2 != 0 for j in range(1,5) if j % 2 ==0]
    [(1, 4), (1, 16), (9, 4), (9, 16)]

    注意:上面是列表解析中,任意数量嵌套的for循环同时关联可选的if 测试,其中if 表示测试语句是可选的,如果没有的话,注意for上下之间表示的是一个嵌套关系。

  • 相关阅读:
    Java 编程下的并发线程之间的同步代码块死锁
    Java 编程下的同步代码块
    Android 编程下两种方式注册广播的区别
    Java 编程下泛型的内部原理
    Android 编程下使用 Google 的 Gson 解析 Json
    c# 调用音库开发英语单词记忆本
    在.Net中实现RichClient+Restful+JPA架构探索实现
    Extjs 常用正则式
    企业内IT部门的一些问题总结
    WinServer2003部署VS2010的水晶报表
  • 原文地址:https://www.cnblogs.com/yangyangchunchun/p/7269502.html
Copyright © 2020-2023  润新知