• python之列表操作(list)


     1 # 列表操作功能汇总
     2 print("列表操作功能汇总")
     3 list_demo = ['first', 'second', 'thrid', 'fourth']
     4 # 复制list_demo列表取名list
     5 list = list_demo[:]
     6 print("原列表为:", list)
     7 print("-----------------------------")
     8 
     9 print("输出列表第一个元素:", list[0])
    10 print("输出列表最后一个元素:", list[-1])
    11 print("从2个开始到第3个输出列表的元素:", list[1:3])
    12 print("从2个开始到末尾输出列表的元素:", list[1:])
    13 
    14 print("-----------------------------")
    15 
    16 # 修改列表指定位置的值
    17 list = list_demo[:]
    18 list[1] = 2
    19 print("修改后的列表:", list)
    20 
    21 # 在列表末尾添加元素
    22 list = list_demo[:]
    23 list.append("hello")
    24 print("列表中插入元素至末尾:", list)
    25 
    26 # 列表中指定位置插入元素
    27 list = list_demo[:]
    28 list.insert(1, "hello")
    29 print("列表中指定位置插入元素:", list)
    30 
    31 print("-----------------------------")
    32 
    33 # 删除列表中指定位置的元素
    34 list = list_demo[:]
    35 del list[1]
    36 print("删除列表中指定位置的元素:", list)
    37 
    38 # 删除列表中指定位置的元素并记录
    39 list = list_demo[:]
    40 popone = list.pop(1)
    41 print("删除列表中指定位置的元素并记录:", list, "; 删掉的元素是:", popone)
    42 
    43 # 删除列表中指定值的元素
    44 list = list_demo[:]
    45 list.remove("first")
    46 print("删除列表中指定值的数据:", list)
    47 
    48 print("-----------------------------")
    49 # 列表解析:将for循环和表达式的代码合并成一行
    50 list = [value ** 2 for value in range(1, 5)]
    51 print("列表解析结果:", list)
    52 
    53 print("-----------------------------")
    54 # 检查列表中是否有指定的元素:in或not in。
    55 list = list_demo[:]
    56 if "first" in list:
    57     print("判断'first'在列表中")
    58 
    59 print("-----------------------------")
    60 # 判断列表中是否有值
    61 if list:
    62     print("判断列表中有值。")
    63 else:
    64     print("判断列表为空。")

    运行结果:

     1 列表操作功能汇总
     2 原列表为: ['first', 'second', 'thrid', 'fourth']
     3 -----------------------------
     4 输出列表第一个元素: first
     5 输出列表最后一个元素: fourth
     6 从2个开始到第3个输出列表的元素: ['second', 'thrid']
     7 从2个开始到末尾输出列表的元素: ['second', 'thrid', 'fourth']
     8 -----------------------------
     9 修改后的列表: ['first', 2, 'thrid', 'fourth']
    10 列表中插入元素至末尾: ['first', 'second', 'thrid', 'fourth', 'hello']
    11 列表中指定位置插入元素: ['first', 'hello', 'second', 'thrid', 'fourth']
    12 -----------------------------
    13 删除列表中指定位置的元素: ['first', 'thrid', 'fourth']
    14 删除列表中指定位置的元素并记录: ['first', 'thrid', 'fourth'] ; 删掉的元素是: second
    15 删除列表中指定值的数据: ['second', 'thrid', 'fourth']
    16 -----------------------------
    17 列表解析结果: [1, 4, 9, 16]
    18 -----------------------------
    19 判断'first'在列表中
    20 -----------------------------
    21 判断列表中有值。
  • 相关阅读:
    (转)Spring中的事务操作
    (转)Spring使用AspectJ进行AOP的开发:注解方式
    (转)SpringMVC学习(十一)——SpringMVC实现Resultful服务
    (转)淘淘商城系列——maven工程debug调试
    日常英语---200120(Some penguins are ticklish)
    心得体悟帖---200120(娱乐和工作同时进行)(觉得有用才会做)
    日常英语---200119(fall)
    日常英语---200119(American Cancer Society Reports Largest Ever Drop in US Cancer Deaths)
    心得体悟帖---200119(不同的选择有不同的烦恼)
    心得体悟帖---200119(玩游戏是正常的,及时制止就好)
  • 原文地址:https://www.cnblogs.com/gongxr/p/7223703.html
Copyright © 2020-2023  润新知