zoukankan      html  css  js  c++  java
  • 小甲鱼-010-012列表

    python列表可以没有类型限制,可以存放不同类型的数据,如整型、浮点型、字符串、对象、列表、tuple

    1.创建列表

    #普通列表
    autos = ['一汽', '东风','上汽','长安', '广汽']
    #混合列表
    mix_autos = [1, '大雄', ['机器猫','小夫'],(1,3),{'龙珠':'比克','铁甲小宝':'卡布达'}]
    #空列表
    empty = []
    

    2.添加元素

    append追加,extend添加一个列表,insert指定位置添加

    empty = ['蛇精', '蝎子精', '穿山甲', '青蛙','瓢虫']
    #追加
    empty.append('葫芦娃')
    #追加个列表
    empty.extend(['大娃','二娃'])
    #指定位置添加
    empty.insert(1, '爷爷')
    print(empty)
    

    3.删除元素

    remove pop del

    empty = ['大娃', '二娃', '三娃', '四娃','五娃', '六娃', '七娃']
    #根据列表的值删除
    empty.remove('七娃')
    #pop默认删除最后一个,也可指定索引
    a = empty.pop()
    empty.pop(4)
    #根据索引删除列表的值
    del empty[0]
    print(empty)
    #删除整个列表
    del empty
    

    4.切片

    empty = ['大娃', '二娃', '三娃', '四娃','五娃', '六娃', '七娃']
    
    #切片选取部分列表,可以指定初始位置和结束位置,不指定就选取整个列表
    list1 = empty[1:3]
    list2 = empty[:5]
    list3 = empty[3:]
    list4 = empty[:]
    print(list1)
    print(list2)
    print(list3)
    print(list4)
    

    5.常用操作符

    5.1比较操作符 > < ==

    list1 = [123,456]
    list2 = [234,567]
    list3 = [123,567]
    print(list1 < list2)
    print(list1 < list3)
    print(list1 == list2)
    print(list1 == list3)
    

    5.2逻辑操作符 and or not

    list1 = [123,456]
    list2 = [234,567]
    list3 = [123,567]
    print((list1 < list2) and (list1 < list3))
    print((list1 == list2) or (list1 < list3))
    print(not (len(list2) > 3))
    

    5.3连接操作符 +

    list1 = [123,456]
    list2 = [234,567]
    list3 = list1 + list2
    print(list3)

    5.4重复操作符 *

    list1 = [123,456]
    list3 = list1 * 3
    list1 *= 2
    print(list1)
    print(list3)
    

    5.5成员关系操作符 in

    empty = ['大娃', '二娃', '三娃', '四娃','五娃', '六娃', '七娃']
    print('大娃' in empty)
    print('蛇精' not in empty)
    mix_autos = [1, '大雄', ['机器猫','小夫']]
    print('机器猫' in mix_autos[2])
    

    6.列表的内置方法

    len列表长度
    count查看值在列表中出现的次数
    index返回值在列表中的索引
    reverse 反序
    sort(reverse=False)

    empty = [9,7,13,85,3,67]
    empty *= 2
    print(empty)
    print(empty.count(9))
    print(empty.index(7))
    print(empty.index(13,3,9))
    #排序
    empty.sort()
    #反向排序
    empty.sort(reverse=True)
    print(empty)
    #反向
    empty.reverse()
    print(empty)
    

    ?

    FAQ

    复制列表要使用切片,因为直接复制列表并不会生成新列表,依旧指向旧列表。针对列表的所有操作都会被记录

  • 相关阅读:
    610K图纸打印新版增值税发票不完整的调整方法 黑盘红盘都兼容
    Teleport Ultra 下载网页修复
    IIS7配置Gzip压缩 JS压强失败的原因
    Destoon B2B 调优SQL后 生成首页仍然慢或不成功的原因
    奈学教育《大数据架构师》课程大纲
    奈学教育《P7架构师》课程大纲
    奈学教育《Java资深研发工程师》课程大纲
    奈学教育《百万架构师》课程大纲
    奈学教育《大数据开发工程师》课程大纲
    奈学教育<P7架构师>课程大纲(第一阶段)
  • 原文地址:https://www.cnblogs.com/csj2018/p/10095129.html
Copyright © 2011-2022 走看看