python学习二----内置数据结构
2017-02-17 21:47
288 查看
Python内置数据结构学习1. 列表List列表list可以实现增加,删除,查找操作>>> list = ['dog','cat','mouse'];>>> list['dog','cat','mouse']1-1. 列表添加元素,append(value)>>> list.append('monkey');>>>list['dog','cat','mouse','monkey']1-2. 列表元素的删除,remove(value)>>> list.remove('cat'); # 如果删除的元素不存在,则会ValueError错误>>> list['dog','mouse','monkey']1-3. 列表元素的插入, insert(index, value)>>> list.insert(1, 'donkey');>>> list['dog','donkey','mouse','monkey']1-4. 删除并返回指定元素, pop(index)pop(): 删除list的最后一个元素,并返回该元素pop(index): 删除指定下标的元素并返回该元素>>> list['dog', 'donkey', 'mouse', 'monkey']>>> list.pop(); # 没有给定下标,则删除list的最后的元素,并返回该元素'monkey'>>> list['dog', 'donkey', 'mouse']>>> list.pop(1); # 指定删除下标为1的元素,并返回该元素'donkey'>>>list['dog','mouse']1-5. 列表元素的查找 index(value)>>> list = ['dog','cat','mouse','monkey']>>> list.index('cat'); # 在list中查找‘cat’元素,返回list中‘cat’的下标2>>> list.index('cat',2); # 查找list中,从下标2开始到最后,查找cat元素ValueError # 如果查找不到则返回错误>>> list.index('cat',1,3); # 下标1-3的元素中查找cat元素11-6. 列表元素的排序 sort()*reverse默认为False,表示按照首字母升序排列,如果设置reverse=True,表示按照首字母降序排列*>>> list = ['dog', 'cat', 'mouse', 'ant']>>> list.sort() # 默认按照首字母生序排列['ant', 'cat', 'dog', 'mouse']>>> list.sort(reverse=True); # reverse=True,按照首字母降序排列['mouse', 'dog', 'cat', 'ant']1-7. 列表元素的翻转 reverse();>>>list = ['dog', 'cat', 'mouse', 'ant']>>>list.reverse()>>>list['ant', 'mouse', 'cat', 'dog']1-8. 列表的索引 list[index]>>> list = ['dog', 'cat', 'mouse', 'ant']>>> list[1] # 输出下标为1的元素'cat'>>> list[-2] # 输出从后往前下标为2的元素'mouse'1-9. 列表的子列表 list[start:end]
>>> list = ['dog', 'cat', 'mouse', 'ant']>>> list[1:3] # 下标1-3的子列表['cat', 'mouse', 'ant']>>> list[:2] # 下标0-2的子列表['dog', 'cat']>>> list[2:] # 下表2-list.length的子列表['mouse','ant']1-10. 列表元素的遍历
相关文章推荐
- python学习3-内置数据结构3-字符串
- python学习6-内置数据结构4-字典
- python学习3-内置数据结构2-元组
- python学习3-内置数据结构3-bytes和bytearray
- linux下python学习笔记(十二)数据结构2
- Python学习笔记——基本数据结构
- Python和数据结构学习 --- 0
- Python和数据结构学习 --- 1
- python内置的数据结构
- python学习笔记(五)之内置容器List
- python数据结构学习记录
- 深入学习Python内置数据类型之——字典(Dictionary)
- 学习python(二) 数据结构
- Python学习(一):入门篇:python中的一些数据结构
- Python和数据结构学习 --- 3
- 深入学习Python内置数据类型之——字典(Dictionary)
- 学习python笔记——数据结构
- [Python学习第一天]内置数据类型介绍,万物皆对象!
- Python和数据结构学习 --- 3
- python学习笔记之内置数据类型