Python-列表与元组

发布时间:2019-03-22 21:57:09编辑:auto阅读(1890)

     

    ·序列

     笔记:

    Python中,最基本的数据结构为序列。序列中每个元素都有编号,其位置或索引,其中第一个元素索引为0,第二个为1,以此类推

    Python内置多种序列,其中包含列表、元组、字符串等,他们都可以称为“容器”(字典也称作容器)。

    注意:列表可以修改,而元组和字符串则不能修改(可以重新赋值)。

     

    @通用的序列操作:


     1 索引:访问列表中的元素

    正向索引:

    x = "请输入带有符号的温度值:"
    print(x[1])
    打印结果: 输
    x = "请输入带有符号的温度值:"[1]
    print(x)
    打印结果: 输

    反向索引:

    x = "请输入带有符号的温度值:"
    print(x[-1])
    打印结果: :

    切片:访问多个特定范围内的元素

    x = "请输入带有符号的温度值:"
    print(x[0:3])
    打印结果: 请输入
    x = "请输入带有符号的温度值:"
    print(x[-3:-1])
    打印结果: 度值 (包左不包右)
    x = "请输入带有符号的温度值:"
    print(x[-1:-3])
    打印结果:
    (值为空,切片使用反向索引序号时,应从小到大,应改为print(x[-3:-1])

    切片遍历整个序列:

    x = "请输入带有符号的温度值:"
    print(x[:])
    打印结果: 请输入带有符号的温度值: ([:]遍历整个序列)

     序列步长:

    x = "请输入带有符号的温度值:"
    print(x[0:12:2]) #步长为2(步长不能为0,否者无法向前移动,可以为负数,即从右向左提取元素。 
    打印结果:
    请入有号温值

    笔记:切片提供两个索引来指定切片范围,第一个索引的元素包含在切片内,第二个索引指定的元素不包含在切片内。(包左不包右)

    相加:序列的拼接

    字符串拼接:

    x = "hello"
    y = ",world"
    print(x+y)
    打印结果: hello,world

    序列拼接

    x = [1,2,3]
    y = [4,5,6]
    print(x+y)
    打印结果: [
    1, 2, 3, 4, 5, 6]

    字符串和序列拼接

    x = "hello"
    y = [4,5,6]
    print(x+y)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can only concatenate str (not "list") to str

    笔记:不同的数据类型不能进行拼接。

    相乘:创建n个空元素

    x = [None]*10
    print(x)
    打印结果: [None, None, None, None, None, None, None, None, None, None]

     成员资格:检查特定的值是否包含在序列中,innot in(返回值为bool值)

    x = "abcdefg"
    print('w' not in x)
    打印结果: True
    x = "abcdefg"
    print('a' in x)
    打印结果: True

     

    ·列表(list

    笔记:

    list():它实际上是一个类,有很多特有的方法。

    使用range函数创建一个列表:

    l = list(range(10))  #创建一个0到9的数(python的索引从0开始,包左不包右)
    print(l)
    打印结果:
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

     将字符串转换为列表:

    x = list("hello")
    print(x)
    打印结果: [
    'h', 'e', 'l', 'l', 'o']

    将列表转换为字符串:

    x = ['h', 'e', 'l', 'l', 'o']
    print(''.join(x))
    打印结果:
    hello

     

    @列表的基本操作:


     1 修改列表:给元素赋值

    x = [1,3,3]
    x[1] = 2
    print(x)
    打印结果: [
    1, 2, 3]

    2 删除列表中的元素

    x = [1,3,3]
    del x[1]
    print(x)
    打印结果: [
    1, 3]

    3 切片赋值

    插入:

    x = [1,5]
    x[1:1] = [2,3,4] #[1:1]在索引 1 后插入[1,2,3]
    print(x)
    打印结果: [
    1, 2, 3, 4, 5]

    替换:

    x = list("persons")
    x[1:] = list("ython") #[1:]可将索引 1 后的切片全部替换
    print(x)
    打印结果: [
    'p', 'y', 't', 'h', 'o', 'n']

     

    @列表中的方法


    1 append:添加一个对象到末尾

    x = [1,2,3,4,5]
    x.append(6)
    print(x)
    打印结果:
    [1,2,3,4,5,6]

     2 pop:删除末尾最后一个元素

    x = [1,2,3,4,5]
    x.pop()
    print(x)
    打印结果:
    [1,2,3,4]

     3 clear:清空表格

    x = [1,2,3,4,5]
    x.clear()
    print(x)
    打印结果:
    []

    4 copy:复制列表

    x = [1,2,3,4,5]
    y = x.copy()
    print(y)
    打印结果:
    [1,2,3,4,5]

    5 count:计算列表中特定元素出现了多少次

    x = [1,2,3,4,5,2,2]
    y = x.count(2)
    print(y)
    打印结果:
    3

    6 extend:将多个值添加到列表末尾(需要区分与拼接的不同,拼接不改变x值)

    x = [1,2,3]
    y = [4,5,6]
    x.extend(y)
    print(x)
    print(y)
    打印结果:
    [1,2,3,4,5,6]
    [4,5,6]

    7 insert:在指定索引插入一个对象(可读性优于extend)

    x = [1,2,3,5]
    x.insert(3,4) #在索引3插入一个数字4
    print(x)
    打印结果:
    [1,2,3,4,5]

    8 index:查找指定值第一次出现的索引

    x = [1,2,3,4,5,2,2]
    y = x.index(2)
    print(y)
    打印结果:
    1

    9 remove:删除指定值的元素

    x = [1,2,3,4,0,5]
    x.remove(0) #删除括号中的值
    print(x)
    打印结果:
    [1,2,3,4,5]

    10 reverse:反向排序列表中的元素

    x = [1,2,3,4,5]
    x.reverse()
    print(x)
    打印结果:
    [5,4,3,2,1]

    11 sort:接受key和reverse两个参数,对列表中的数进行有逻辑的排序(永久排序)

    顺序:

    x = [5,2,1,4,3]
    x.sort()
    print(x)
    打印结果:
    [1, 2, 3, 4, 5]

    倒序:

    x = [5,2,1,4,3]
    x.sort(reverse = True)
    print(x)
    #打印结果:
    [5, 4, 3, 2, 1]

    按元素长度排序:

    x = ['apple','beer','to','g','banana']
    x.sort(key=len)
    print(x)
    #打印结果:
    ['g', 'to', 'beer', 'apple', 'banana']

    12 sorted函数:对列表中的数进行有逻辑的排序(临时排序

    x = [5,2,1,4,3]
    y = sorted(x)
    print(y)
    print(x)
    打印结果:
    [1,2,3,4,5]
    [5,2,1,4,3]  #不改变原列表排序

    13 len函数:获取列表的长度

    x = [5,2,1,4,3]
    print(len(x))
    打印结果:
    5

     

    ·元组(tuple)

    笔记:

      元组也是序列,与、列表的区别在于元组不能修改

    print(tuple([1,2,3]))
    print(tuple("abcdefg"))
    #打印结果:
    (1, 2, 3)
    ('a', 'b', 'c', 'd', 'e', 'f', 'g')

    将元组赋值给x:

    x = 1,2,3
    print(x)
    #打印结果:
    (1, 2, 3)

    创建多个元组:

    x = 3 * (40+2,)
    print(x)
    #打印结果:
    (42, 42, 42)

    元组中只有一个值时,也必须在它后面加逗号(,):

    x = (2,)
    print(x)
    #打印结果:
    (2,)

     

    end~

     ****** 几米花的Python ****** 博客主页:https://www.cnblogs.com/jimmy-share/  欢迎转载 ~

关键字