Python 中 list 与 tuple (列表与元组)
2018-03-07 17:22
483 查看
Python
list 列表、
有序、存储多个元素、可使用索引、索引从0开始、可以截取、插入、删除、修改、一个列表可以作为另一个列表中的元素>>> list_name=['a','b','c',1,2,3]
通过索引获取列表中的元素:list_name[ ]
>>> list_name[0] 'a' >>> list_name[-1] 3 >>> list_name[2:3] ['c'] >>> list_name[2:2] [] >>> list_name[3:-1] [1, 2] >>> list_name[2:6] ['c', 1, 2, 3] >>> list_name[2:9] ['c', 1, 2, 3] >>> list_name[2:] ['c', 1, 2, 3]
向列表中插入元素:append() 向列表的尾部添加一个新的元素 / insert()将元素插入到索引指定的位置
>>> list_name.append('z') >>> print(list_name) ['a', 'b', 'c', 1, 2, 3, 'z'] >>> list_name.insert(3,'d') >>> print(list_name) ['a', 'b', 'c', 'd', 1, 2, 3, 'z']
删除列表中的元素:pop()删除列表中的元素 / del 删除列表中的元素
>>> list_name.pop() 'z' >>> print(list_name) ['a', 'b', 'c', 'd', 1, 2, 3] >>> list_name.pop(3) 'd' >>> print(list_name) ['a', 'b', 'c', 1, 2, 3] >>> del list_name[5] >>> print(list_name) ['a', 'b', 'c', 1, 2]
修改列表中的元素:根据索引替换某个元素/一个列表可以做为另一个列表中的元素
>>> list_name[0]='z' >>> print(list_name) ['z', 'b', 'c', 1, 2] >>> list1=[True,'哈','q',2] >>> print(list_name) ['z', 'b', 'c', 1, 2] >>> list_name[1]=list1 >>> print(list_name) ['z', [True, '哈', 'q', 2], 'c', 1, 2] >>> list_name[1][1] '哈'
Python列表函数&方法
函数:
1 cmp(list1, list2) 比较两个列表的元素
2 len(list) 列表元素个数
3 max(list)返回列表元素最大值
4 min(list) 返回列表元素最小值
5 list(seq) 将元组转换为列表
方法:
1 list.append(obj) 在列表末尾添加新的对象
2 list.count(obj) 统计某个元素在列表中出现的次数
3 list.extend(seq) 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
4 list.index(obj) 从列表中找出某个值第一个匹配项的索引位置
5 list.insert(index, obj) 将对象插入列表
6 list.pop(obj=list[-1]) 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
7 list.remove(obj) 移除列表中某个值的第一个匹配项 8 list.reverse() 反向列表中元素
9 list.sort([func]) 对原列表进行排序
tuple 元组、
元组与列表类似、元组的元素不能修改、元组使用圆括号、列表使用方括号、任意无符号的对象、以逗号隔开、默认为元组
元组中只包含一个元素时,需要在元素后面添加逗号
>>> tuple1=('a','b','c',1,2,3) >>> print(tuple1) ('a', 'b', 'c', 1, 2, 3) >>> tuple2=(1) >>> print (tuple2) 1 >>> tuple3=(1,) >>> print(tuple3) (1,) >>> tuple4=() >>> print(tuple4) ()
可以使用索引获取元组中的元素:
>>> tuple1[0] 'a' >>> tuple1[1:5] ('b', 'c', 1, 2)
del删除整个元组:
>>> del tuple1 >>> print(tuple1) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'tuple1' is not defined
元组内置函数
1 cmp(tuple1, tuple2) 比较两个元组元素。
2 len(tuple) 计算元组元素个数。
4 min(tuple)返回元组中元素最小值。
5 tuple(seq) 将列表转换为元组。
相关文章推荐
- python基础学习二 list和tuple(列表和元组)
- python中str(字符串)、list(列表)、tuple(元组)、dict(字典)相互转化关系及字典键-值遍历
- Python学习笔记(9)-列表list、元组tuple、字典dict、集合set
- python 列表list 元组tuple 字典dict 集合set 介绍
- Python学习笔记---元组(tuple)、列表(list)、字典(dict)
- Python体验(03)-列表list和元组tuple
- python3基础: 元组tuple、 列表list、 字典dict、集合set。 迭代器、生成器
- Python中list(列表)、tuple(元组)、dict(字典)基本操作快速入门
- python中的集合详解(列表List,元组Tuple,字典,set)
- Python中列表(list)与元组(tuple)的对比
- python的列表(List),元组(Tuple),集合(Set),字典(Dict)
- python之列表(list),元组(tuple),集合(set),字典(dict)
- Python学习整理之 列表list 元组tuple 字典dictionary
- Python数据结构 字符串,列表list,元组tuple,字典dict,容器
- Python中的列表(list),元组(Tuple),字典(Dict)和集合(Set)
- Python list 列表和tuple元组
- python 中的 List(列表) 和 tuple(元组)的区别
- python:列表list和元组tuple
- Python3的列表list和元组tuple初学总结(未完)
- Python 中 list 与 tuple (列表与元组)