python 里list, tuple, set, dict的异同
2015-09-08 10:09
483 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/pilicurg/article/details/48287195
list和tuple
list和tuple都是sequence的一种,是有序列表,其内置的方法都相似,
>>> lst = [1, 2, 3, 4, 5] >>> tpl = (1, 2, 3, 4, 5)
比如支持in运算,
>>> 1 in lst True >>> 1 in tpl True >>>
元素有坐标,
>>> lst.index(2) 1 >>> tpl.index(2) 1 >>>
支持index
>>> lst[3] 4 >>> tpl[3] 4 >>>
支持slicing
>>> lst[2:4] [3, 4] >>> tpl[2:4] (3, 4) >>>
list和tuple的区别:list是mutable的对象,内容可以更改,tuple则不是,所以是hashable的。关于mutable和hashable的概念,可以参考Python 里 immutable和hashable的概念
所以,一些list有的方法,在tuple里就不能实现:
>>> lst.append(6) >>> lst [1, 2, 3, 4, 5, 6] >>> tpl.append(6) Traceback (most recent call last): File "", line 1, in tpl.append(6) AttributeError: 'tuple' object has no attribute 'append' >>> >>> lst.pop() 6 >>> tpl.pop() Traceback (most recent call last): File "", line 1, in tpl.pop() AttributeError: 'tuple' object has no attribute 'pop' >>>
set和dict
set和dict的都是无序列表,没有index的概念。
相关文章推荐
- Python学习笔记-四类数据存储结构:list、tuple、dict以及set
- python之列表(list),元组(tuple),集合(set),字典(dict)
- Python基础知识- list ,tuple ,dict 和 set
- python——内置类型——list tuple dict set
- python学习-------list、tuple、set、dict
- python的dict,set,list,tuple应用详解
- 廖雪峰python3.6教程笔记3-Python基础之使用list, tuple, dict和set
- python的dict,set,list,tuple简单分析
- Python中list,tuple,dict,set的区别和用法
- Python - list, tuple, dict, set的区别
- 【廖雪峰Python学习笔记】list & tuple & dict &set
- Python中set、list、dict、tuple的比较
- 【python】list tuple dict set 用法及说明
- python中list/tuple/dict/set的区别
- python基础之list+tuple+dict+set
- Python中关于str,list,tuple,set,dict的区别和联系
- python中的list、tuple、dict辨析,再加上set的辨析。
- python中 list和tuple 、dict和set区别
- python的dict,set,list,tuple应用
- Python基础(二)List和tuple、条件判断、dict和set