美文网首页
关于Python的列表和元组

关于Python的列表和元组

作者: StarDustMrsu | 来源:发表于2019-05-21 15:52 被阅读0次

    python的列表和元组

    基本概念

    list:可以放置任意数据类型的动态的元素指向可变的有序集合,创建之后可以随意增删改元素。
    tuple:可以放置任意数据类型的静态的元素指向不可变的有序集合,创建之后无法增删改元素。
    主要区别:动态还是静态,元素指向可变还是不可变!

    list01 = [1,2,3,4]
    list01[3] = 444
    list01
    tuple01 = (1,2,3,4)
    tuple01[3] = 444
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-5-3781e7f64cf2> in <module>()
          5 list01
          6 tuple01 = (1,2,3,4)
    ----> 7 tuple01[3] = 444
    
    TypeError: 'tuple' object does not support item assignment
    对一个tuple进行这种操作直接报错!
    

    基本操作

    list

    创建list
    使用[]和函数list()都可以创建列表,但是[]更高效!
    import timeit
    print(timeit.timeit("[x for x in range(0,10000)]", number=10000))
    print(timeit.timeit("list(x for x in range(0,10000))", number=10000))
    3.7451904740009923
    7.075908677012194
    
    将其他数据类型转换成list
    print(timeit.timeit("a_tuple = ('ready','fire','pool');[a_tuple]", number=10000))
    print(timeit.timeit("a_tuple = ('ready','fire','pool');list(a_tuple)", number=10000))
    感觉还是用[]更高效!
    0.0006942460022401065
    0.0024700629874132574
    
    list的切片操作
    citys = ["BeiJing","NewYork","Singapore","Toronto"]
    citys[0] = "Shanghai"
    new_citys = citys
    print(new_citys[0:2])
    print(new_citys[:])
    print(new_citys[:2])
    print(new_citys[0:3:2])
    print(new_citys[-1])
    
    ['Shanghai', 'NewYork']
    ['Shanghai', 'NewYork', 'Singapore', 'Toronto']
    ['Shanghai', 'NewYork']
    ['Shanghai', 'Singapore']
    Toronto
    list和tuple都支持切片操作和负数索引!
    
    list的常用操作
    #append在末尾添加元素
    new_citys.append("Beijing")
    print(new_citys)
    #合并list
    import timeit
    print(timeit.timeit("list001=[];list001.extend([x for x in range(1,100000)])",number=1000))
    print(timeit.timeit("list001=[];list001+[x for x in range(1,100000)]", number=1000))
    ['Shanghai', 'NewYork', 'Singapore', 'Toronto', 'Beijing']
    4.620128600974567
    4.220356322999578
    
    #insert在指定位置插入元素
    citys = ["BeiJing","NewYork","Singapore","Toronto"]
    citys.insert(0,"Tokyo")
    print(citys)
    #del删除元素
    del citys[0]
    print(citys)
    #remove删除元素
    citys.remove("Toronto")
    print(citys)
    #pop删除元素
    citys.pop()
    print(citys)
    ['Tokyo', 'BeiJing', 'NewYork', 'Singapore', 'Toronto']
    ['BeiJing', 'NewYork', 'Singapore', 'Toronto']
    ['BeiJing', 'NewYork', 'Singapore']
    ['BeiJing', 'NewYork']
    
    用append把元素添加到尾部,用pop从尾部删除元素这种后进先出的数据结构叫栈stack。
    用append把元素添加到尾部,用pop(0)从首部删除元素这种后进后出的数据结构叫队列。
    
    #使用index查询特定值的位置
    citys = ["BeiJing","NewYork","Singapore","Toronto"]
    citys.index("NewYork")
    #使用in判断元素是否存在
    print(citys)
    'Tokyo' in citys
    #使用count记录特定值出现的次数
    citys.append("Toyko");citys.append("Toyko")
    citys.count("Toyko")
    ['BeiJing', 'NewYork', 'Singapore', 'Toronto']
    2
    
    list和tuple常用的内置函数
    list001 = [3,".",1,4,1,5,9,2,6,5,3,5,8,9,8]
    print(list001.count(5))
    print(list001.index(8))
    list001.reverse()
    print(list001)
    list001_new=[x for x in list001 if type(x) != str];print(list001_new);list001_new.reverse();print(list001_new)
    3
    12
    [8, 9, 8, 5, 3, 5, 6, 2, 9, 5, 1, 4, 1, '.', 3]
    [8, 9, 8, 5, 3, 5, 6, 2, 9, 5, 1, 4, 1, 3]
    [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 8]
    
    tuple001 = (3,".",1,4,1,5,9,2,6,5,3,5,8,9,8)
    print(tuple001.count(5))
    print(tuple001.index(8))
    print(list(reversed(tuple001)))
    tuple001_new=sorted(tuple(x for x in tuple001 if type(x) != str));print(tuple001_new)
    3
    12
    [8, 9, 8, 5, 3, 5, 6, 2, 9, 5, 1, 4, 1, '.', 3]
    [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 8, 8, 9, 9]
    
    count(item)统计list或tuple中item出现的次数。
    index(item)返回list或tuple中item第一次出现的索引。
    list.reverse()原地倒转list,tuple没有这个函数。
    list.sort()排序list,tuple没有这个函数。
    reversed()对list或tuple进行倒转,返回一个处理好的新的list或tuple。
    sorted()对list或tuple进行排序,返回一个处理好的新的list或tuple。
    
    list和tuple的存储差异
    print(list().__sizeof__())
    print(tuple().__sizeof__())
    print(list(x for x in range(0,10000)).__sizeof__())
    print(tuple(x for x in range(0,10000)).__sizeof__())
    print(list(x for x in range(0,100000)).__sizeof__())
    print(tuple(x for x in range(0,100000)).__sizeof__())
    print(list(x for x in range(0,1000000)).__sizeof__())
    print(tuple(x for x in range(0,1000000)).__sizeof__())
    40
    24
    83088
    80024
    879816
    800024
    8250144
    8000024
    由此可见动态的可变的list占用的空间更多!
    
    list和tuple的性能
    #初始化相同元素tuple更快
    import timeit
    print(timeit.timeit("[1,2,3,4,5,6,7,8,9,10]", number=1000000))
    print(timeit.timeit("(1,2,3,4,5,6,7,8,9,10)", number=1000000))
    0.11689231399213895
    0.014740854036062956
    #索引操作基本
    import timeit
    print(timeit.timeit("a=[1,2,3,4,5,6,7,8,9,10];b=a[9]", number=1000000))
    print(timeit.timeit("a=(1,2,3,4,5,6,7,8,9,10);b=a[9]", number=1000000))
    0.14129851799225435
    0.03787975601153448
    

    总结

    tuple相比list更加轻量化,性能稍优,但是增删改list更优!

    相关文章

      网友评论

          本文标题:关于Python的列表和元组

          本文链接:https://www.haomeiwen.com/subject/cqujzqtx.html