Python列表、元组、字典的异同

Python列表、元组、字典的异同

2023年7月26日发(作者:)

Python列表、元组、字典的异同Python列表、元组、字典的异同1,列表:list可变的数据类型,可以被改变,可以进⾏嵌套处理,可在⼀个列表中存储⼀个序列的项⽬指明⼀个列表的⽅法是:使⽤⽅括号代码⽰例:>>> fruit_list = ['apple', 'pear', 'orange', 'banana', 'watermetton', 'strawberry']>>> lenrth = len(fruit_list)>>> print lenrth6>>> for items in fruit_list: print items,

apple pear orange banana watermetton strawberry>>> fruit_('pawpaw') ###添加⼀个元素>>> fruit_('apple') ###列表中删除⼀个元素不能⽤.del(),⽽是.remove()SyntaxError: invalid syntax>>> fruit_('aple')Traceback (most recent call last): File "", line 1, in fruit_('aple')ValueError: (x): x not in list>>> fruit_('apple')>>> fruit_()'pawpaw'>>> fruit_('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_('mouse') ##元组是不可修改的Traceback (most recent call last): File "", line 1, in animal_('mouse')AttributeError: 'tuple' object has no attribute 'append'>>> animal_('dog') ##嵌套在元组内的元组元素⽆法检索到Traceback (most recent call last): File "", line 1, in animal_('dog')ValueError: (x): x not in tuple>>> animal_('fish') ##元组的开始元素的标号是003,字典把键和值联系在⼀起,其中键必须是唯⼀的,键和值之间⽤冒号:进⾏分隔字典的表⽰⽅法:使⽤{},键和值之间⽤冒号“:”隔开,项之间⽤逗号“,”隔开代码⽰例:>>> 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_(change_items) ##.update()更新字典内容>>> items_dict{'Snowy': '67845', 'Alice': '23456', 'Robin': '55667', 'Tom': '67890'}>>> 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。

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1690307672a329894.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信