zoukankan      html  css  js  c++  java
  • python学习(二) 列表和元组

    第2章  列表和元组

    2.1 序列概论

    python有六种内建的序列,本章讨论最常用的两种类型:列表和元组。其他的内建序列有字符串、Unicode字符串、buffer对象和xragne对象。

    列表和元组的主要区别是:列表可以修改,元组不能修改。

    1)列表
    list1 = ['Google', 'Runoob', 1997, 2000];
    list2 = [1, 2, 3, 4, 5 ];
    list3 = ["a", "b", "c", "d"];
    2)元组
    tup1 = ('Google', 'Runoob', 1997, 2000);
    tup2 = (1, 2, 3, 4, 5 );
    tup3 = "a", "b", "c", "d";
    >>> edward=['fsdfsd','fsdfs']
    >>> edward
    ['fsdfsd', 'fsdfs']
    >>> john=['gfdg',45]
    >>> john
    ['gfdg', 45]
    >>> database=[edward, john]
    >>> database
    [['fsdfsd', 'fsdfs'], ['gfdg', 45]]

    注意:序列(比如列表和元组)映射(例如字典)是两类主要容器。序列中每个元素都有自己的编号,而映射中每个元素都有一个名字。而集合既不是序列也不是映射。

    2.2 通用序列操作

    2.2.1 索引

    >>> greetin = 'hello'
    >>> greetin[0]
    'h'

    可以使用负数进行索引,最后一个元素的位置是-1

    >>> test='hello'
    >>> test[-1]
    'o'
    >>>

    字符串字面值可以直接使用索引:

    >>> 'hello'[1]
    'e'

    如果一个函数调用返回一个序列,那么可以直接对返回结果进行索引操作,

    >>> test = input()[3]
    abcdeft
    >>> test
    'd'

    >>> endings = ['st','nd','rd'] +17*['th']
    + ['st','nd','rd'] + 7*['th']
    + ['st']
    >>> endings
    ['st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st']

    2.2.2 分片

    >>> tag='<a href = "http://www.python.org">Python web site</a>'
    >>> tag[9:30]         //  从位置9到位置30(注意:9在分片内,30不在内
    ' "http://www.python.o'

    >>> tag[9:-4]         //  从位置9到位置-4(注意:9在分片内,-4不在分片内

    ' "http://www.python.org">Python web site'

    >>> numbers=[1,2,3,4,5,6,7,8,9,10]
    >>> numbers[0:1]
    [1]
    >>> numbers[7:10]  // 10不存在,它前面一个元素包含在分片中
    [8, 9, 10]
    >>>

    >>> numbers[-3:]   // 从-3开始的所有元素
    [8, 9, 10]

    >>> numbers[:3]    // 从开始到3的元素
    [1, 2, 3]

    >>> numbers[:]     // 整个序列
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    如何设置步长:

    >>> numbers[0:10:2]     //从0到10,步长是2的方式访问元组
    [1, 3, 5, 7, 9]

    步长不能是0,但是可以是负数:

    >>> numbers[10:0:-1]    // 如果步长是负数,需要序列的开头大于序列的结尾;如果步长是正数,需要开头小于结尾
    [10, 9, 8, 7, 6, 5, 4, 3, 2]

    2.2.3 序列相加

    >>> [1,2,3] + [4,5,6]    // 序列可以相加
    [1, 2, 3, 4, 5, 6]

    >>> "hellow" +"world" // 字符串可以相加
    'hellowworld'

    >>> [1,2,3] + "world!"    // 不同类型不可以相加
    Traceback (most recent call last):
    File "<pyshell#59>", line 1, in <module>
    [1,2,3] + "world!"
    TypeError: can only concatenate list (not "str") to list

    2.2.4 乘法

    >>> 'python' *5                           //  序列乘法,序列被重复N次
    'pythonpythonpythonpythonpython'

    None: python内建值,确切含义是“这里什么都没有”。所以,如果想初始化一个长度为10的列表,可以按照下面的列子来实现:

    >>> seq = [None]*10
    >>> seq
    [None, None, None, None, None, None, None, None, None, None]
    >>>

    2.2.5 成员资格

    in: 检查一个值是否在序列中

    >>> permission = 'rw'

    >>> 'r' in permission
    True

    >>> users = ['mlh','foo','bar']
    >>> input() in users
    mlh
    True
    >>>

    >>> subject = '&&&&&fdfsfsdfsdfsdf&'
    >>> "&&&" in subject
    True

    2.2.6 长度、最小值和最大值

    len:包含元素的数量

    min: 最大值

    max:最小值

    >>> numbers=[100,34,678]
    >>> len(numbers)
    3
    >>> max(numbers)
    678
    >>> min(numbers)
    34

    2.3 列表:Python的“苦力”

    2.3.1 list函数

    因为字符串不能像列表一样被修改,所以有时根据字符串创建列表会很有用。list函数可以实现:

    >>> var = list("hello")
    >>> var
    ['h', 'e', 'l', 'l', 'o']

    2.3.2 基本的列表操作

    (1)改变列表:元素赋值

    >>> x=[1,2,3]
    >>> x[1] = 8
    >>> x
    [1, 8, 3]

    (2)删除元素:

    >>> x
    [1, 8, 3]
    >>> del x[1]
    >>> x
    [1, 3]

    >>> names = ['a','b','c']
    >>> del names[1]
    >>> names
    ['a', 'c']

    (3)分片赋值:

    >>> name=list("perl")
    >>> name
    ['p', 'e', 'r', 'l']
    >>> name[2:] = "hhhh"
    >>> name
    ['p', 'e', 'h', 'h', 'h', 'h']

    也可以通过分片来删除元素:

    >>> numbers
    [100, 34, 678]
    >>> numbers[1:2] = []
    >>> numbers
    [100, 678]

    2.3.3 列表方法

    (1) append: 在列表末尾追加新的对象

    >>> list = [1,2,3]
    >>> list.append(4)
    >>> list
    [1, 2, 3, 4]

    注意:append并不是返回新列表,而是直接在原来列表的合适的位置修改。

    (2) count: 统计某个元素在列表中出现的次数

    >>> x = [[1,2],1,1,[2,1,[1,2]]]
    >>> x.count(1)
    2
    >>> x.count([1,2])
    1

    (3) extend:在列表的末尾一次性追加另一个序列中的多个值

    >>> a = [1,2,3]
    >>> b = [4,5,6]
    >>> a.extend(b)
    >>> a
    [1, 2, 3, 4, 5, 6]

    注意:这个操作看起来和连接操作很像,主要区别是:extend方法修改了被扩展的序列;而连接操作返回一个全新的列表。

    也可以用分片的方式来实现extend的功能:

    >>> a = [1,2,3]
    >>> b = [4,5,6]
    >>> a[len(a):] = b
    >>> a
    [1, 2, 3, 4, 5, 6]

    (4) index: 从列表中找出某个值第一个匹配项的索引位置

    >>> knights = ['we','are','the']
    >>> knights.index('we')
    0

    (5) insert: 将对象插入到列表中

    >>> numbers = [1,2,3,5,6,7]
    >>> numbers.insert(3,"fd")
    >>> numbers
    [1, 2, 3, 'fd', 5, 6, 7]           // 注意:列表中元素的类型可以有多种

    insert方法也可以用赋值来实现:

    >>> numbers = [1,2,3,5,6,7]
    >>> numbers[3:4] = ["fsdfsdf"]
    >>> numbers
    [1, 2, 3, 'fsdfsdf', 6, 7]

    (6) pop

    pop方法会移除列表中的一个元素,并且返回该元素的值

    >>> x = [1,2,3]
    >>> x.pop()      // POP 是唯一一个可以既能修改列表又返回元素值的列表方法。
    3
    >>> x

    (7) remove: 移除列表中某个值的第一个匹配项

    >>> x = ['to','be','or','not','to','be']
    >>> x.remove('to')
    >>> x
    ['be', 'or', 'not', 'to', 'be']

    (8) reverse:将列表中的元素反向存放

    >>> x = [1,2,3]
    >>> x.reverse()    // 改变了列表但不返回值
    >>> x       

    (9) sort: 

    >>> x = [1,4,2,6,3]
    >>> x.sort()          // 在原位置排序,改变序列的值
    >>> x
    [1, 2, 3, 4, 6]

    如果获得一个列表的排序副本,而不改变原来列表的内容呢?

    第一种方法:

    >>> x = [5,2,6,7]
    >>> y = x[:]          // 注意:这里不能用y = x, 如果那样的话,y和x指向同样的内容。
    >>> y.sort()
    >>> y
    [2, 5, 6, 7]
    >>> x
    [5, 2, 6, 7]

    另外一种方法:

    >>> x = [5,1,7,4]
    >>> y = sorted(x)
    >>> x
    [5, 1, 7, 4]
    >>> y
    [1, 4, 5, 7]

    (10) 高级排序: 按照自定义的比较函数进行排序

    sort方法有另外两个可选的参数:key和reverse.如果要使用它们,就通过名字来指定。

    x.sort(key = len)   // 传len作为键函数,根据元素长度进行排序。

    x.sort(reverse=True)   //  是否进行反向排序

    2.4 元组:不可变序列

    元组与列表一样,也是一种序列,唯一的不同是元组不能修改。

    >>> 1,2,3          // 用逗号分隔了一些值,就是元组。
    (1, 2, 3)

    >>> x= 1,2,3
    >>> x
    (1, 2, 3)

    >>> 42
    42
    >>> 43,                // 只有一个值的元组,一定要有逗号,
    (43,)

    >>> x = (42)              // 逗号很重要,只添加圆括号是没有用的
    >>> x
    42
    >>> x = (43,) 
    >>> x
    (43,)

    >>> 3*(40+2)
    126
    >>> 3*(40+2,)
    (42, 42, 42)

    2.4.1 tuple函数:把一个序列转成元组

    >>> tuple([1,2,3])
    (1, 2, 3)
    >>> tuple('aab')
    ('a', 'a', 'b')
    >>> tuple((1,2,3))
    (1, 2, 3)

    2.4.2 基本元组操作:

    >>> x = 1,2,3
    >>> x[0]
    1
    >>> x[0:2]
    (1, 2)

    2.4.3 元组的意义何在?

    有两个重要原因,元组是不可以替代的:

    (1) 元组可以在映射(和集合的成员)中当做键使用,而列表则不行

    (2)元组作为很多内建函数和方法的返回值存在。

    2.5 小结

    序列:典型的序列包括列表,字符串和元组。列表是可变的,而元组和字符串是不可变的。分片可以访问序列的一部分

    成员资格: in

  • 相关阅读:
    Oracle的分区打点
    学习Struts2经验总结
    优化MVC,实现数据库表的记录的添加、删除、修改、查询。
    基于struts研究传值问题
    基于“MVC”框架集设计模式,开发用户管理系统!
    使用Struts,实现简单的登录
    QT学习4:使用窗口部件
    QT学习9:绘制函数
    QT学习8:准备战斗
    QT学习6:组装丰富的积木!
  • 原文地址:https://www.cnblogs.com/liufei1983/p/7199912.html
Copyright © 2011-2022 走看看