[python] list和tuple的异同
2013-11-13 21:51
302 查看
【声明】
list:
l = ['a', 'b']
tuple:
t = 'a', 'b' 或 t = ('a', 'b')
当只有0个元素时:
empty = ()
当只有1个元素时:
singleton = 'hello', 或 singleton = ('hello',),注意,不能是singleton = ('hello')
【用法】
两者都是[sequence] data type, 很相似,区别在于:
1. list是mutable的,而tuple是immutable的(尽管可以保存mutable的对象,如t = ['a','b'], [1,2])。
2. list主要用于相同类型的数据存储,访问的主要方式是通过for迭代访问;
而tuple主要用于不同类型的数据存储,访问的主要方式是通过下标,或者通过unpacking
unpacking是?
list:
l = ['a', 'b']
tuple:
t = 'a', 'b' 或 t = ('a', 'b')
当只有0个元素时:
empty = ()
当只有1个元素时:
singleton = 'hello', 或 singleton = ('hello',),注意,不能是singleton = ('hello')
【用法】
两者都是[sequence] data type, 很相似,区别在于:
1. list是mutable的,而tuple是immutable的(尽管可以保存mutable的对象,如t = ['a','b'], [1,2])。
2. list主要用于相同类型的数据存储,访问的主要方式是通过for迭代访问;
而tuple主要用于不同类型的数据存储,访问的主要方式是通过下标,或者通过unpacking
unpacking是?
相关文章推荐
- Python---list和tuple的异同
- 【python】详解python元组tuple与列表List异同
- python 列表list 元组tuple 字典dict 集合set 介绍
- Python之List,Tuple,Set,Dict
- Python set list dict tuple 区别
- python学习:list&tuple
- 关于python数据类型tuple中嵌入list的尝试与思考
- python列表list,元组tuple,字典dict
- python31初级[list+tuple+set+dict+str+file的成员方法]
- python语法 --list,tuple, str格式化,iterable
- Python中list,tuple,dict用法
- python 集合类型 tuple 与 list
- Python list, dict, set, tuple
- Python中list,tuple,dict,set的区别和用法
- python中 list和tuple 、dict和set区别
- python is、==区别;with;gil;python中tuple和list的区别;Python 中的迭代器、生成器、装饰器
- LeetCode-39-Combination Sum, 回溯dfs,Python的list.append()覆盖前面,tuple,set
- Python3数据结构【列表list,元组tuple,集合set】
- 【python笔记】python中的list、tuple、set、dict用法简析
- Python中list,tuple,dict的区别