zoukankan      html  css  js  c++  java
  • 大爽Python入门教程 2-2 序列: 字符串、元组与列表

    大爽Python入门公开课教案
    点击查看教程总目录

    序列

    序列(sequence): 顾名思义,有序的排列。
    有序排列的一串数据。
    一种容器,容器内成员有序排列。

    python的字符串str,元组tuple,列表str都是序列。

    所以字符串str,元组tuple,列表str有些操作是相通的,
    都支持序列操作。

    序列内的成员个数,一般也称为序列的长度。
    可以使用len(sequence)函数获取,
    其中sequence为序列变量。

    使用示例:

    >>> len("abcde")
    5
    >>> len('')
    0
    >>> a_tuple = (3, 4)
    >>> len(a_tuple)
    2
    >>> a_list = [1, 0, 0, 1]
    >>> len(a_list)
    4
    

    索引

    索引(index): 序列中元素的位置的值(编号)。
    可以通过索引来获取对应位置的数据。
    写法为sequence[index]
    python的索引从0开始,
    即序列第一个值的索引为0,
    第二个值的索引为1,
    第三个值的索引为2。
    以此类推。。。

    由于索引从0开始,
    也有人喜欢把第一个值称为第0个值。
    但是这样会导致交流时理解混乱。
    比如一个人说第一个值,到底是列表开头的那个,还是索引为1的那个。
    所以为了准确与避免混淆,表达时最好专门说明其索引为几
    比如列表中索引为1的值...
    而第几个值,还是按照常规意思来理解。

    超出索引会报错

    IndexError: ... index out of range
    

    使用示例:
    字符串

    >>> s = "abc"
    >>> s[0]
    'a'
    >>> s[1]
    'b'
    >>> s[2]
    'c'
    >>> s[3]
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    IndexError: string index out of range
    

    元组

    >>> t = ("li hua", 18)
    >>> t[0]
    'li hua'
    >>> t[1]
    18
    >>> t[2]
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    IndexError: tuple index out of range
    

    列表

    >>> numbers = ["one", 2.0, "3", 4, "five"]
    >>> numbers[0]
    'one'
    >>> numbers[1]
    2.0
    >>> numbers[2]
    '3'
    >>> numbers[3]
    4
    >>> numbers[4]
    'five'
    >>> numbers[5]
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    IndexError: list index out of range
    

    可以通过
    sequence.index(value)也可以得到值对应的索引
    但是如果值没出现过的话,会报错ValueError

    示例如下:

    >>> s = "abc cba"
    >>> s.index("a")
    0
    >>> s.index("c")
    2
    >>> s.index("d")
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    ValueError: substring not found
    

    倒序索引

    python中索引可以是负数,代表倒着数的序号。
    倒数第一个索引是-1,
    倒数第二个索引是-2,
    倒数第三个索引是-3,
    以此类推。。。
    示例

    >>> numbers = ["one", 2.0, "3", 4, "five"]
    >>> numbers[-1]
    'five'
    >>> numbers[-2]
    4
    >>> numbers[-5]
    'one'
    >>> numbers[-6]
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    IndexError: list index out of range
    

    切片

    切片切片,切出一片。从序列中切出一段,
    或者说截取出一段,更准确的来说是复制了一段
    得到一个新的序列。
    注:切片操作不会改变原来的序列。

    切片操作的基础语法是sequence[start:end]
    取出从start这个索引开始(包含start索引对应的值),
    一直到end这个索引为止(不包含end索引对应的值)的这一段。

    >>> s = "abcdefg"
    >>> s[1]
    'b'
    >>> s[1:4]
    'bcd'
    >>> s[4]
    'e'
    >>> s[4:6]
    'ef'
    

    start和end都可以不写,
    不写start代表从开头开始,
    不写end代表没有end(从start开始后一直取完后面所有的)。

    >>> s[:3]
    'abc'
    >>> s[0:3]
    'abc'
    >>> s[4:]
    'efg'
    

    切片操作时,
    start和end可以超出序列长度,不会报错。

    >>> s[4:7]
    'efg'
    >>> s[4:10]
    'efg'
    >>> s[7:10]
    ''
    

    start甚至可以大于end,
    不过此时取出来的是空(start等于end时也是空)。

    >>> s[3:1]
    ''
    >>> s[3:3]
    ''
    

    解包Unpack

    解包Unpack语法不是序列专属的,很多容器都支持这个语法。

    解包这个概念比较生僻,不好理解。
    那么这么想:
    首先,pack是打包的意思,
    比如把几个苹果装到一个箱子容器里面
    Unpack就是这个过程反过来,
    把装有苹果的箱子里的苹果全部取出来。

    示例:

    >>> loc = (3, 4)
    >>> x, y = loc  # Unpack
    >>> x
    3
    >>> y
    4
    

    上面的x, y = loc就是解包元组
    英语来讲就是Unpack the tuple

    解压时,序列中有多少个元素,左边就要有对应个数的变量。
    数量不匹配就会报错ValueError

    >>> a,b,c = loc
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    ValueError: not enough values to unpack (expected 3, got 2)
    

    解包Unpack的语法,一般是tuple用。
    虽然字符串和列表也可以用,但是由于其长度一般不确定,
    所以一般不是用这个语法。

    修改

    注意:
    字符串和元组都是不可变对象,不可以修改。
    列表是可变对象,可以修改。
    修改字符串和元组会报错TypeError
    对于列表,可以使用索引来修改对应索引的值。
    其语法为list[index]=new_value

    示例如下

    >>> s = "abcde"
    >>> s[0] = "d"
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    TypeError: 'str' object does not support item assignment
    >>> lst = [1, 2, 3, 4, 5]
    >>> lst[3] = 6
    >>> lst
    [1, 2, 3, 6, 5]
    >>> lst[0], lst[2] = lst[2], lst[0]
    [3, 2, 1, 6, 5]
    

    最后2句补充说明: 先执行等号右边的,
    再把等号右边的赋值给等号左边的。

    python的字符串是不可变对象,不能直接修改,
    有人可能碰到过一些方法,看起来好像修改了字符串。
    这些方法本质上都是生成了一个新的字符串。

    拓展说明:
    对于元组来讲,成员对象不能变,
    但是变量的属性修改是可以的。
    这是比较复杂的东西,暂不深究。

    拼接

    序列之间可以使用加法,表示拼接,
    会得到一个新的拼到一起的序列。
    示例

    >>> "123" + "456"
    '123456'
    >>> (1, 2) + (3, 4)
    (1, 2, 3, 4)
    >>> ["a", 12] + ["b", 20]
    ['a', 12, 'b', 20]
    
  • 相关阅读:
    学习笔记008之Task
    问题: ActivityManager: Warning: Activity not started, its current task has been brought to the front
    问题; No label views point to this text field with an android:labelFor="@+id/@+id/editTextNumber1" attribute
    学习笔记001之[Android开发视频教学].01_06_Android当中的常见控件
    学习笔记002之学习资料
    学习笔记001之环境配置
    设计模式之代理模式
    C# Enum转换
    Json数据序列化对象,及对象序列化为Json格式
    groupby以后取每组前n行
  • 原文地址:https://www.cnblogs.com/BigShuang/p/15161467.html
Copyright © 2011-2022 走看看