Python 列表
python 列表
列表的特点
1、列表是一种可变的数据类型,这点是跟元组有区别的
2、列表中的值是有序的,并且可存放重复的值,这点跟set有区别的
3、python中的列表类似于其它语言中的数组
4、列表中元素的值可以多种数据类型并存
列表基本操作方法
1、元素赋值
|
1
2
3
|
>>> list1 = [3,9,'python','java',[9,3,5]]>>> print list1[3, 9, 'python', 'java', [9,3,5]] |
2、列表分片
|
1
2
3
4
5
6
7
8
9
10
11
|
#列表中的索引从0开始,第一个冒号前面的值代表列表索引的起始值,第一个冒号后面的值表示分片结束的索引值(不包含)>>>print list1[0:4][3, 9, 'python', 'java']#[::-1] 第二个冒号后面的值表示步长,可为负,负数表示按照步长倒序输出>>>print list1[::2]>>>print list1[::2][3, 'python', [9, 3, 5]]>>>print list1[::-1][[9, 3, 5], 'java', 'python', 9, 3]>>>print list1[-1:-4:-2][[9, 3, 5], 'python'] |
3、删除元素
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>> print list1[3, 9, 'python', 'java', [9, 3, 5]]#remove方法是根据列表中的值进行删除>>> list1.remove(3)#如果remove的值在列表中的值不存在,那么会解释器会毫不留情的报错>>> remove(1)Traceback (most recent call last): File "<input>", line 1, in <module>NameError: name 'remove' is not defined>>> print list1[9, 'python', 'java', [9, 3, 5]]#del是根据索引进行删除>>> del list1[2]>>> print list1[9, 'python', [9, 3, 5]] |
4、len方法
|
1
2
3
4
5
6
7
|
>>> list1 = [1,8,23,56,32,9,23,[5,9,3,9]]#内嵌列表在外面列表看来长度为1>>> print len(list1)8#计算内嵌列表的长度>>> print len(list1[-1])4 |
5、count方法
|
1
2
3
4
5
6
7
8
|
>>> print list1.count(23)2#如果要统计的值在列表中不存在则返回0>>> print list1.count(25)0#内嵌列表以及元组的值不会被统计进来>>> print list1.count(9)1 |
6、append方法
|
1
2
3
4
5
6
|
#append可以添加各种数据类型>>> list1.append('Kingway')>>> list1.append([2,3,4,5])>>> list1.append(9)>>> print list1[1, 8, 23, 56, 32, 9, 23, [5, 9, 3, 9], 'Kingway', [2, 3, 4, 5], 9] |
7、extend方法
|
1
2
3
4
5
6
7
8
9
|
#extend属于列表的扩展,只能对其他列表进行扩展,而无法像append那样添加其它元素的值>>> list1.extend([6,7,8])>>> print list1[1, 8, 23, 56, 32, 9, 23, [5, 9, 3, 9], 'Kingway', [2, 3, 4, 5], 9, 6, 7, 8]#直接添加其它元素的值会报错>>> list1.extend(9)Traceback (most recent call last): File "<input>", line 1, in <module>TypeError: 'int' object is not iterable |
8、insert方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#insert方法中第一个参数的值为索引,第二个参数为需要插入的值,插入值可以是各种数据类型>>> list1.insert(3,'four')>>> print list1[1, 8, 23, 'four', 56, 32, 9, 23, [5, 9, 3, 9], 'Kingway', [2, 3, 4, 5], 9, 6, 7, 8]#如果索引的值大于列表长度,那么将在列表的最后一位插入该值>>> list1.insert(100,"end")>>> print list1[1, 8, 23, 'four', 56, 32, 9, 23, [5, 9, 3, 9], 'Kingway', [2, 3, 4, 5], 9, 6, 7, 8, 'end']#索引的值不能为负,否则将会报错>>> list1.index(-10,'first')Traceback (most recent call last): File "<input>", line 1, in <module>TypeError: slice indices must be integers or None or have an __index__ method |
9、pop方法
|
1
2
3
4
5
6
7
8
|
#pop方法用于删除相应的值并将删除的值返回,默认删除列表的最后一位>>> list1.pop()'end'#删除并返回指定位置的值>>> list1.pop(2)23>>> print list1[1, 8, 'four', 56, 32, 9, 23, [5, 9, 3, 9], 'Kingway', [2, 3, 4, 5], 9, 6, 7, 8] |
10、sort方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#sort方法可以将列表进行排序>>> list1 = [23,9,15,3]>>> list1.sort()>>> print list1[3, 9, 15, 23]#sort方法会改变原列表的值,sorted方法不会对原列表的值进行修改,sort为列表内置的方法,sorted为python内置的方法>>> list1 = [23,9,15,3]>>> list2 = sorted(list1)>>> print list1[23, 9, 15, 3]>>> print list2[3, 9, 15, 23]#sort可以以字符串值的长度进行排序>>> list1 = ['a','dddd','ccc','bb']>>> list1.sort(key = len)>>> print list1['a', 'bb', 'ccc', 'dddd'] |
11、其它方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#reverse方法,将列表中的元素倒序排列>>> list1 = [23,5,9,18,6]>>> list1.reverse()>>> print list1[6, 18, 9, 5, 23]#max方法,将列表中的最大值输出>>> print max(list1)23#min方法,将列表中的最小值输出>>> print min(list1)5#index方法,将对应元素的索引输出,如果该元素在列表中不存在,则报错,如果该元素在列表中有多个,则返回第一个被查到的值的索引值>>> list1 = [32,8,234,9,8,12]>>> list1.index(8)1>>> list1.index(3)Traceback (most recent call last): File "<input>", line 1, in <module>ValueError: 3 is not in list#cmp方法,比较两个列表的值,如果第一个大于第二个则返回1,反之结果为-1,两个列表相等则为0>>> list1 = [9,5]>>> list2 = [9,7]>>> cmp(list1,list2)-1>>> list2 = [9,5]>>> cmp(list1,list2)0>>> list2 = [7,9]>>> cmp(list1,list2)1 |
Python 列表的更多相关文章
- python 列表排序
转自http://www.iplaypython.com/jinjie/jj114.html reverse()方法 将列表中元素反转排序,比如下面这样>>> x = [1,5,2, ...
- python列表、元祖、字典
python列表 ['a','1','vs2'] 里面的值可以改 python元祖 ('a','1','css','sdf12') 里面的值不能改 python字典 {'s ...
- Python列表、元组、字典和字符串的常用函数
Python列表.元组.字典和字符串的常用函数 一.列表方法 1.ls.extend(object) 向列表ls中插入object中的每个元素,object可以是字符串,元组和列表(字符串“abc”中 ...
- python 列表生成器
python 列表生成器 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 一个循环 在C语言等其他语言中,for循环一般是这样的 ...
- [转载] Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_star ...
- Python 列表如何获得一个指定元素所在的下标
在使用Python3.4读取txt数据到列表,由于编码问题,读取到的数据通常会出现'\ufeffX'(x为你想要的数据).这时候如果需要把列表中的数据由字符串转换到数值型的数据的进行数据分析的话就会出 ...
- python列表的常用操作方法
主要介绍了Python中列表(List)的详解操作方法,包含创建.访问.更新.删除.其它操作等,需要的朋友可以参考下. 1.创建列表.只要把逗号分隔的不同的数据项使用方括号括起来即可 List = [ ...
- !!对python列表学习整理列表及数组详细介绍
1.Python的数组分三种类型:(详细见 http://blog.sina.com.cn/s/blog_6b783cbd0100q2ba.html) (1) list 普通的链表,初始化后可以通过特 ...
- (转载)Python 列表(list)操作
(转载)http://blog.csdn.net/facevoid/article/details/5338048 创建列表sample_list = ['a',1,('a','b')] Python ...
随机推荐
- oracle job create table insert into
create or replace procedure proc_tzyj is begin insert into t_trade_activity@dw3_link.regress.rdbms.d ...
- post multipart data boundary问题 使用curl 向jersey post文件
原以为curl 模拟post file跟post string类似,-d参数一加 ,header一加就完了,这次遇到个问题,却怎么都搞不定. curl模拟post提交 与客户端定的协议是: Heade ...
- ssh下:系统初始化实现ServletContextListener接口时,获取spring中数据层对象无效的问题
想要实现的功能:SSH环境下,数据层都交由Spring管理:在服务启动时,将数据库中的一些数据加载到ServletContext中缓存起来. 系统初始化类需要实现两个接口: ServletContex ...
- net中序列化读写xml
参考http://www.cnblogs.com/fish-li/archive/2013/05/05/3061816.html 我们可以直接使用XmlTextReader.XmlDocument.X ...
- 我的qq邮箱的GPG公钥
呵呵,赶一波潮流,我的邮箱 175420840@qq.com 的GPG公钥如下,也可以在这里直接下载.具体可参见阮一峰的<GPG入门教程>. -----BEGIN PGP PUBLIC K ...
- android 缓存Bitmap 使用内存缓存
private LruCache<String, Bitmap> mMemoryCache; /** * 判断内存大小 设置位图的缓存空间 */ private void judgeMem ...
- Jmeter—3 http请求—content-type与参数
本文讲三种content-type以及在Jmeter中对应的参数输入方式 第一部分:目前工作中涉及到的content-type 有三种: content-type:在Request Headers里, ...
- __attribute__ 你知多少?
GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和 ...
- Ubuntu安装gfortran
命令行运行 sudo apt-get install gfortran
- YHMMR003 农户基本信息的维护程序
*********************************************************************** * Title : * * Application : ...