• Python_列表,元组和字典的异同


    1,列表:list

    可变的数据类型,可以被改变,可以进行嵌套处理,可在一个列表中存储一个序列的项目

    指明一个列表的方法是:使用方括号

    代码示例:

    >>> fruit_list = ['apple', 'pear', 'orange', 'banana', 'watermetton', 'strawberry']
    >>> lenrth = len(fruit_list)
    >>> print lenrth
    6
    >>> for items in fruit_list:
        print items,
    
        
    apple pear orange banana watermetton strawberry
    >>> fruit_list.append('pawpaw')                     ###添加一个元素
    >>> fruit_list.del('apple')                        ###列表中删除一个元素不能用.del(),而是.remove()
    SyntaxError: invalid syntax
    >>> fruit_list.remove('aple')
    
    Traceback (most recent call last):
      File "<pyshell#10>", line 1, in <module>
        fruit_list.remove('aple')
    ValueError: list.remove(x): x not in list
    >>> fruit_list.remove('apple')
    >>> fruit_list.pop()
    'pawpaw'
    >>> fruit_list.count('apple')                    ###.count()函数不是统计列表的长度,而是查找列表中某个元素出现的次数,Len(list)用于计算列表长度
    0
    >>> print fruit_list
    ['pear', 'orange', 'banana', 'watermetton', 'strawberry']

    2,元组

    和列表类似,但是元组是不可修改的,可以进行嵌套

    指明一个元组的方法:使用圆括号,中间使用“ , ”将项目分隔

    创建一个元组:使用逗号分隔,或者使用tuple()函数 eg:1,2,3  ;tuple([1,2,3])

    代码示例:

    >>> animal_tuple = ('fish', 'snake', 'wolf',('dog', 'cat', 'sheap'))
    >>> len(animal_tuple)                  ##求解元组的长度
    4
    >>> 1,2,3                              ##创建一个元组
    (1, 2, 3)
    >>> tuple([1,2,3])
    (1, 2, 3)
    >>> 
    >>> animal_tuple.append('mouse')           ##元组是不可修改的
    
    Traceback (most recent call last):
      File "<pyshell#20>", line 1, in <module>
        animal_tuple.append('mouse')
    AttributeError: 'tuple' object has no attribute 'append'
    >>> animal_tuple.index('dog')             ##嵌套在元组内的元组元素无法检索到!!!!!
    
    Traceback (most recent call last):
      File "<pyshell#21>", line 1, in <module>
        animal_tuple.index('dog')
    ValueError: tuple.index(x): x not in tuple
    >>> animal_tuple.index('fish')               ##元组的开始元素的标号是0
    0

    3,字典

    把键和值联系在一起,其中键必须是唯一的,键和值之间用冒号:进行分隔

    字典的表示方法:使用{},键和值之间用冒号“:”隔开,项之间用逗号“,”隔开

    代码示例:

    >>> phonebook_dict = {'Alice': '23456', 'Tom':'67890', 'Snowy': '67845'}    ##创建一个字典
    >>> phonebook_dict
    {'Snowy': '67845', 'Alice': '23456', 'Tom': '67890'}
    >>> items = [('Alice', '23456'), ('Tom','67890'), ('Snowy', '67845')]  ##列表转化为字典
    >>> items_dict = dict(items)
    >>> items_dict
    {'Snowy': '67845', 'Alice': '23456', 'Tom': '67890'}
    >>> len(items_dict)
    3
    >>> change_items = {'Robin': '55667'}
    >>> items_dict.update(change_items) ##.update()更新字典内容
    >>> items_dict
    {'Snowy': '67845', 'Alice': '23456', 'Robin': '55667', 'Tom': '67890'}
    >>> items_dict.items()   ##字典转化为列表.items()函数
    [('Snowy', '67845'), ('Alice', '23456'), ('Robin', '55667'), ('Tom', '67890')]

    4:总结

    把序列转化为列表:.list(seq)             [1,2,3,4]   >>>>.append()  .remove()  .pop() .index()    .count()  len()

    把序列转化为元组:.tuple(seq)          (1,2,3,4)   >>>>>.index()  .count()    len()

    把序列转化为字典: .dict(seq)           {'1' : 'Snowy', '2':'Tom', '3': 'Alice'}    >>>>.update()  .pop()  len()

    字典转化为列表:   items(dict)  

    PS:string相当于字符元组

           array:只能存储同种数据类型的数据,区别于list,相比较list使用的空间更小,通过 from array import array 可以使用array模块

    zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表

         code:

    >>> a = ['1', '2']
    >>> b = ['a', 'c', 'd']
    >>> zip(a,b)
    [('1', 'a'), ('2', 'c')]
    >>> zip(*zip(a,b))
    [('1', '2'), ('a', 'c')]
    >>> a = [[1,2,3],[4,5,6],[7,8,9]]
    >>> zip(a)
    [([1, 2, 3],), ([4, 5, 6],), ([7, 8, 9],)]
    >>> print [row[0] for row in a]
    [1, 4, 7]
    >>> zip(*a)
    [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

    (*)操作符与zip函数配合可以实现与zip相反的功能,即将合并的序列拆成多个tuple。

             

    每天多一点提高,给自己一些激励,开心生活,用编码来丰富我的生活,加油! ↖(^ω^)↗
  • 相关阅读:
    python并发编程之深入理解yield from
    python中的多进程编程
    Python并发concurrent、Future类、异步
    【Socket通信】关于Socket通信原理解析及python实现
    深入理解Python元类
    Django RestFrameWork 源码解析
    Django的Restful规范
    小程序colorUI框架初步使用教程
    类似按键精灵的鼠标键盘录制和自动化操作 模拟点击和键入 (KeyMouseGo)
    如何在postgresql中,一句sql完成未有数据记录的insert,再update的操作
  • 原文地址:https://www.cnblogs.com/graceting/p/3632585.html
Copyright © 2020-2023  润新知