zoukankan      html  css  js  c++  java
  • 011.Python的列表的相关操作

    一 列表的相关操作

    1.1  列表的拼接

    lst1 = [1,2,3]
    lst2 = [4,5,6]
    res = lst1 + lst2
    print(res)

    执行

    [root@node10 python]# python3 test.py
    [1, 2, 3, 4, 5, 6]

    1.2 列表的重复

    lst1 = [7,8,9]
    res = lst1 * 3
    print(res)

    执行

    [root@node10 python]# python3 test.py
    [7, 8, 9, 7, 8, 9, 7, 8, 9]

    1.3 列表的切片

    语法 => 列表[::] 完整格式:[开始索引:结束索引:间隔值]

    1. [开始索引:] 从开始索引截取到列表的最后
    2. [:结束索引] 从开头截取到结束索引之前(结束索引-1)
    3. [开始索引:结束索引] 从开始索引截取到结束索引之前(结束索引-1)
    4. [开始索引:结束索引:间隔值] 从开始索引截取到结束索引之前按照指定的间隔截取列表元素值
    5. [:]或[::] 截取所有列表

    开始索引

    listvar = ["刘备","关羽","张飞","曹操","许褚","荀彧","诸葛亮","赵云","黄忠"]
    #从开始索引截取到列表的最后
    res= listvar[1:]
    print(res)

    执行

    [root@node10 python]# python3 test.py
    ['关羽', '张飞', '曹操', '许褚', '荀彧', '诸葛亮', '赵云', '黄忠']

    结束索引

    listvar = ["刘备","关羽","张飞","曹操","许褚","荀彧","诸葛亮","赵云","黄忠"]
    #从开头截取到结束索引之前(结束索引-1)
    res = listvar[:4]
    print(res)

    执行

    [root@node10 python]# python3 test.py
    ['刘备', '关羽', '张飞', '曹操']

    开始索引:结束索引

    listvar = ["刘备","关羽","张飞","曹操","许褚","荀彧","诸葛亮","赵云","黄忠"]
    # (3)[开始索引:结束索引]  从开始索引截取到结束索引之前(结束索引-1)
    res = listvar[4:6]
    print(res)

    执行

    [root@node10 python]# python3 test.py test.py
    ['许褚', '荀彧']

    开始索引:结束索引:间隔值

    listvar = ["刘备","关羽","张飞","曹操","许褚","荀彧","诸葛亮","赵云","黄忠"]
    # 0 3 6 9   先把数字数出来,通过下标找对应值
    res = listvar[::3]
    print(res)
    # 3 5 7 9
    print(listvar[3::2])
    # 0 3 6
    print(listvar[:5:3])

    执行

    [root@node10 python]# python3 test.py test.py
    ['刘备', '曹操', '诸葛亮']
    ['曹操', '荀彧', '赵云']
    ['刘备', '曹操']

    倒叙

    listvar = ["刘备","关羽","张飞","曹操","许褚","荀彧","诸葛亮","赵云","黄忠"]
    print(listvar[::-1])
    # -2 -4
    print(listvar[-2:-6:-2])

    执行

    [root@node10 python]# python3 test.py test.py
    ['黄忠', '赵云', '诸葛亮', '荀彧', '许褚', '曹操', '张飞', '关羽', '刘备']
    ['赵云', '荀彧']

    1.4 列表的获取

    listvar = ["刘备","关羽","张飞","曹操","许褚","荀彧","诸葛亮","赵云","黄忠"]
    res = listvar[2]
    print(res)
    res = listvar[-1]
    print(res)

    执行

    [root@node10 python]# python3 test.py test.py
    张飞
    黄忠

    1.5  列表的修改

    listvar = ["刘备","关羽","张飞","曹操","许褚","荀彧","诸葛亮","赵云","黄忠"]
    listvar[3] = "123"
    print(listvar)
    # 如果使用了切片进行修改,右侧必须是可迭代性数据(容器类型数据)
    # listvar[2:] = "123"
    listvar[2:] = range(0,4)
    print(listvar)

    执行

    [root@node10 python]# python3 test.py test.py
    ['刘备', '关羽', '张飞', '123', '许褚', '荀彧', '诸葛亮', '赵云', '黄忠']
    ['刘备', '关羽', 0, 1, 2, 3]

    指定间隔

    listvar = ["刘备","关羽","张飞","曹操"]
    # 如果使用了切片,并且指定了第三个参数间隔值,那么截取几个,就放几个,一一对应
    listvar[::2] = ("a","b")
    print(listvar)

    执行

    [root@node10 python]# python3 test.py test.py
    ['a', '关羽', 'b', '曹操']

    1.6 列表的删除

    listvar = ["刘备","关羽","张飞","曹操","许褚","荀彧","诸葛亮","赵云","黄忠"]
    del listvar[1]
    print(listvar)
    
    # 可以使用切片删除
    del listvar[1:3]
    print(listvar)

    执行

    [root@node10 python]# python3 test.py test.py
    ['刘备', '张飞', '曹操', '许褚', '荀彧', '诸葛亮', '赵云', '黄忠']
    ['刘备', '许褚', '荀彧', '诸葛亮', '赵云', '黄忠']

    元组当中的一级数据更改不了,但是二级或者多级如果是列表这样的数据可修改

    tup = (1,2,3,4,5,[1,2,3,45])
    tup[-1][-1] = 54
    print(tup)

    执行

    [root@node10 python]# python3 test.py test.py
    (1, 2, 3, 4, 5, [1, 2, 3, 54])

    二 列表的相关函数

    1 append

    功能:向列表的末尾添加新的元素
    格式:列表.append(值)
    返回值:None
    注意:新添加的值在列表的末尾,该函数直接操作原有列表

    2.1 增加函数

    2.1.1 append

    listvar = [1,2,3,4,5]
    listvar.append(6)
    print(listvar)
    listvar.append([12,3])
    print(listvar)

    执行

    [root@node10 python]# python3 test.py test.py
    [1, 2, 3, 4, 5, 6]
    [1, 2, 3, 4, 5, 6, [12, 3]]

    2.1.2 insert

    功能:在指定索引之前插入元素
    格式:列表.insert(索引,值)
    返回值:None
    注意:直接改变原有列表

    示例

    listvar = [1,2,3,4,5]
    listvar.insert(2,7)
    print(listvar)

    执行

    [root@node10 python]# python3 test.py test.py
    [1, 2, 7, 3, 4, 5]

    2.1.3 extend

    功能:迭代追加所有元素
    格式:列表.extend(可迭代性数据)
    返回值:None
    注意:直接改变原有列表

    示例

    listvar = [1,2,3,4,5]
    listvar.extend(("",""))
    # listvar.extend({'a':1,"b":2}) 语法上允许
    print(listvar)

    执行

    [root@node10 python]# python3 test.py test.py
    [1, 2, 3, 4, 5, '', '']

    2.2 删除

    2.2.1  pop

    功能:通过指定索引删除元素,若没有索引移除最后那个
    格式:列表.pop(索引)
    返回值:删除的元素
    (注意:没有指定索引,默认移除最后一个元素 )

    示例

    listvar = [1,2,3,4,5]
    res = listvar.pop()
    print(res)
    print(listvar)
    listvar = [1,2,3,4,5]
    res = listvar.pop(3) # 指定索引
    # res = listvar.pop(33)  删除不存在的报错
    print(listvar)

    执行

    [root@node10 python]# python3 test.py test.py
    5
    [1, 2, 3, 4]
    [1, 2, 3, 5]

    2.2.2  remove

    功能:通过给予的值来删除,如果多个相同元素,默认删除第一个
    格式:列表.remove(值)
    返回值:无
    (注意:如果有索引的情况推荐使用pop,效率高于remove)

    示例

    listvar = [1,2,3,4,5]
    listvar.remove(4)
    print(listvar)

    执行

    [root@node10 python]# python3 test.py test.py
    [1, 2, 3, 5]

    2.2.3 clear

    功能:清空列表
    格式:列表.clear()
    返回值:空列表

    示例

    listvar = [1,2,3,4,5]
    listvar.clear()
    print(listvar)

    执行

    [root@node10 python]# python3 test.py test.py
    []

    2.3 列表其他操作

    改查 具体参数列表相关操作

    2.3.1 index查找

    功能:获取某个值在列表中的索引
    格式:列表.index(值[,start][,end]) # []  表达参数可选项 
    返回值:找到返回索引  (找不到报错)

    示例

    listvar = [1,2,3,4,54,4,90,4,78,78787,7878]
    res = listvar.index(3)
    print(res)
    # res = listvar.index(99) # 如果索引不存在直接报错
    res = listvar.index(4,4) # 5
    print(res)
    res = listvar.index(4,6,9) # 指定索引的查找范围,高位取不到
    print(res)

    执行

    [root@node10 python]# python3 test.py test.py
    2
    5
    7

    2.3.2 count计数

    功能:计算某个元素出现的次数
    格式:列表.count(值)
    返回值:次数

    示例

    listvar = [1,2,3,4,54,4,90,4,78,78787,7878]
    res= listvar.count(4)
    print(res)

    执行

    [root@node10 python]# python3 test.py test.py
    3

    2.3.3 sort排序

    功能:列表排序(默认小到大排序)
    格式:列表.sort(reverse=False)                        
    返回值:None
    注意:直接更改原列表

    示例

    listvar = [78,12,-3,99]
    # 默认从小到大排序 (正序)
    listvar.sort()
    print(listvar)
    # 从大到小排序 用reverse = True (倒叙)
    listvar.sort(reverse=True)
    print(listvar)

    执行

    [root@node10 python]# python3 test.py test.py
    [-3, 12, 78, 99]
    [99, 78, 12, -3]

    2.4.4 reverse反转

    功能:列表反转操作
    格式:列表.reverse()
    返回值:None
    注意:直接更改原列表

    示例

    listvar = [78,12,-3,99]
    listvar.reverse()
    print(listvar)

    执行

    [root@node10 python]# python3 test.py test.py
    [99, -3, 12, 78]
  • 相关阅读:
    uva 408 Uniform Generator
    Java实现 蓝桥杯VIP 算法提高 栅格打印问题
    Java实现 蓝桥杯VIP 算法提高 栅格打印问题
    Java实现 蓝桥杯VIP 算法提高 栅格打印问题
    Java实现 蓝桥杯VIP 算法提高 打水问题
    Java实现 蓝桥杯VIP 算法提高 打水问题
    Java实现 蓝桥杯VIP 算法提高 打水问题
    Java实现 蓝桥杯VIP 算法提高 打水问题
    Java实现 蓝桥杯VIP 算法提高 不同单词个数统计
    Java实现 蓝桥杯VIP 算法提高 不同单词个数统计
  • 原文地址:https://www.cnblogs.com/zyxnhr/p/12271403.html
Copyright © 2011-2022 走看看