• python 学习笔记 二 序列, 列表, 元组, 字符串


    序列

    序类是一个集合概念, Pthon包括六种内建序列: 列表, 元组, 字符串, Unicode字符串, buffer对象和xrange对象. 其中, 我们最长用的要数前三个.

    通用序列操作

      1. 索引(indexing)

    序列的所有元素都是有编号的(从0开始...), 这些元素可以通过编号访问, 如:

    >>> greeting = 'Hello'

    >>> greeting[0]

    'H'

    使用负数索引(从-1开始...)是从右边开始的:

    >>> greeting[-1]

    'o'

    若函数调用返回一个序列, 可以直接对返回结果进行索引:

    >>> fourth = raw_input("Year: ")[3]

    Year: 2014

    '4'

      2. 分片(sliceing)

    使用俩个索引来访问一定范围内的元素:

    >>> tag = '<a href="http://www.python.org">Python web site</a>'

    >>> tag[9:30]

    'www.python.org'

    >>> tag[32, -4]

    'Python web site'

    一些技巧:

    >>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    访问最后三个元素只需要空一个索引:

    >>> numbers[-3:]

    [8, 9, 10]

    同样适用与访问前三个:

    >>> numbers[:3}

    [1, 2, 3]

    若要复制整个序列, 可使用numbers[:]

    改变步长(step length)

    分片时默认的步长是1, 我们可以改变步长:

    >>> numbers[0:10:2]

    [1, 3, 5, 7, 9]

      3. 加法(adding)

    通过加号进行序列相加:

    >>> [1, 2, 3] + [4, 5, 6]

    [1, 2, 3, 4, 5, 6]

    >>> 'Hello, ' + 'world!'

    'Hello, world'

    相同类型的序列才能相加!!!

      4. 乘法(multiplying)

    用数字乘以一个序列会产生新的序列:

    >>> 'python' * 3

    >>> 'pythonpythonpython'

    >>> [42] * 5

    [42, 42, 42, 42, 42]

      5. 成员资格

    使用in运算符检查一个值是否在序列中, 在,返回True, 不在, 返回False.

    >>> name = 'python'

    >>> 'o' in name

    True

    >>> 'x' in name

    False

    >>> 'py' in name

    True

      6.长度, 最大值和最小值

    内建函数len, max, min可用于求序列包含的元素个数, 最大值, 最小值.

    >>> lst = [1, 2, 3]

    >>> len(lst)

    3

    >>> max(lst)

    3

    >>> min(lst)

    1

    列表(list)

    列表是Python中一个非常强大的内建类型, 列表元素写在[ ]中, 下面是一个简单的类表:

      colors = ['red', 'blue', 'green']
      print colors[0]    ## red
      print colors[2]    ## green
      print len(colors)  ## 3

    =赋值运算使俩个变量在内存中指向同一个列表:

    b = colors   ## Does not copy the list

    list函数

    可以根据字符串创建列表

    >>> list('Hello')

    ['H', 'e', 'l', 'l', 'o']

    None, 空列表与初始化

    空列表:[ ]

    Python中有一个内建值Node: 表示'这里上面也没有'

    如果想让一个列表占用10个元素空间, 可以这样: [None] * 10,

    基本列表操作

      1.元素赋值

    >>> num = [1, 2, 3]

    >>> num[1] = 10

    >>> num

    [1, 10, 3]

      2.删除元素

    使用del语句实现删除:

    >>> names = ['Alice', 'Bob', 'Cindy']

    >>> del names[1]

    >>> names

    ['Alice', 'Cindy']

      3. 分片赋值

    基本使用:

    >>> name = list('Perl')

    >>> name

    ['P', 'e', 'r', 'l']

    >>> name[2:] = list('ar')

    name

    ['P', 'e', 'a', 'r']

    可以使用与原序列不等长的序列将分片替换:

    >>> name = list('Perl')

    >>> name[1:] = list('ython')

    >>> name

    [''P, 'y', 't', 'h', 'o', 'n']

    可以在不需要替换的情况下插入新元素:

    >>> numbers = [1, 5]

    >>> numbers[1:1] = [2, 3, 4]

    >>> numbers

    [1, 2, 3, 4, 5]

    这里实质上是替换了空白, 可以将这个思路反过来, 实现删除:

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

    >>> numbers[1, 4] = [ ]

    >>> numbers

    [1, 5]

    列表方法

      1.append

    在列表末追加元素: list.append(elem)

      2.count

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

      3.extend

    在列表末尾一次性追加另一个列表的多个值, 也就是说可以用新列表拓展原有的: list1.extend(list2)

      4.index

    找除某个值在列表中第一次出现的索引位置: list.index(elem), 如果这个值不存在, 会引发异常

      5.insert

    将元素插入到列表的第n个位置之后: list.insert(n, elem), n是从1开是的.

      6.pop

    移除列表的一个元素(默认最后一个), 并返回它的值: list.pop() 或指定位置, list.pop(n)

      7.remove

    移除某个值在列表中的第一个匹配项: list.remove(elem)

      8.reverse

    反向存放列表中的元素: list.reverse()

      9.sort

    在原位置对列表进行排序(默认升序, 原位置意味着改变列表): list.sort()

      10.自定义排序

    元组(tuple)

    元组的特点是不可改变. 元组一般是用圆括号括起来的.

    用逗号分隔一些值创建元组:

    >>> 1, 2, 3

    (1, 2, 3)

    空元组

    >>> ()

    ()

    一个值的元组

    >>> 42,

    (42,)

    逗号不可丢.

    tuple函数

    以一个序列作为参数, 并把它转换成元组, 如果参数就是元组, 原样返回,例:

    >>> tuple([1, 2, 3])

    (1, 2, 3)

    >>> tuple 'abc'

    ('a', 'b', 'c')

    元组存在的意义

      1. 元组可以在映射中充当键(key)值---列表则不行

      2. 元组可以作为很多内建函数和方法的返回值存在

    字符串(string)

    格式化

    在%左侧放置一个字符串, 右侧放置希望个格式化的值.

    >>> format = "Hello, %s, %s enough for ya?"

    >>> values = ('world', 'Hot')

    >>> print format % values

    Hello, world, Hot enough for ya?

    注: 如果格式化串中包含%, 使用%%转义.

    其他:

    -: 表示左对齐, 

    +: 表示不管正数还是负数, 都显示符号

    空格:不足位以空格填充

    0: 不足位以0填充

    字符串方法:

      1.find

    在一个较长的字符串中寻找子串, 返回字串所在位置的最左端索引, 未找到则返回-1: string1.find(string2)

      2.join

    可用特定字符连接字符串列表, 返回字符串, 是split方法的逆运用.

    >>> lst = ['1', '2', '3']  # 不能是[1, 2, 3]

    >>> lst.join('+')

    '1+2+3'

      3.lower(upper)

    将字符串中的所有大写(小写)字母转换为小写(大写)字母: string.lower() string.upper()

      4.replace

    将字符串中所有的匹配串替换: 

    >>> 'This is a test'.replace('is', 'xxx')

    'Thxxx xxx a test'

      5.split

    分割字符串, 是join的逆运用:

    >>> '/usr/bin/python'.split('/')

    [' ', 'usr', 'bin', 'python']

      6.strip

    去除字符串俩侧的空格或制表符(不包括中间的):

    >>> '           Hello world!          '.strip()

    'Hello world!'

      7.tanslate

    与replace类似, 可以替换字符串中的某些部分, 不同的是, translate只处理单个字符

      8.maketrans

    接受俩个参数: 俩个等长的字符串, 表示将第一个字符串中的每个字符都用第二个字符串中相同位置的字符替换.

    >>> from string import maketrans

    >>> table = maketrans('cs', 'kz')

    >>> 'this is an incredible test'.translate(table)

    >>> 'thiz iz inkredible tezt'

    tanslate的第二个参数是可选的, 原来指定需要删除的字符:

    >>> 'this is an incredible test'.translate(table, ' ')

    >>> 'thizizinkredibletezt'

  • 相关阅读:
    python3-file的修改实现类似shell中sed的功能
    python3-字典的循环
    python3-file文件操作
    python3-字典的增删改查
    python3-字典中存储列表
    python3-字典中的一些常用方法
    python3-字典中包含字典
    报错调试和工具使用
    (三)、Struts第三天
    struts体系结构
  • 原文地址:https://www.cnblogs.com/ezhengnan/p/3741675.html
Copyright © 2020-2023  润新知