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 ...
随机推荐
- HTML问题,a href =" "和 a href ="#"这两个有什么区别?
a href ="" 默认打开的还是当前页面,会刷新一下重新打开.a href ="#" 浏览器地址栏网址后面会多显示1个#.不会刷新页面,会回到页面顶部.
- Windows与Linux主机之间的连接和交互工具
1.Putty 远程连接Linux主机 Windows主机上安装putty,工具打开后显示如下: 输入要连接的Linux主机的IP地址,点击Load,连接主机后输入用户名密码,即可登录Linux主机 ...
- SSH集成log4j日志环境[转]
第一步:在web.xml初始化log4j <context-param> <param-name>log4jConfigLocation</param-name> ...
- iOS开发拓展篇—音频处理(音乐播放器5)
iOS开发拓展篇—音频处理(音乐播放器5) 实现效果: 一.半透明滑块的设置 /** *拖动滑块 */ - (IBAction)panSlider:(UIPanGestureRecognizer *) ...
- 【转】request和response的页面跳转传参
下面是一位园友的文章: jsp或Servlet都会用到页面跳转,可以用 request.getRequestDispatcher("p3.jsp").forward(request ...
- Servlet路径跳转2--在servlet当中,跳转到某网页时的路径写法
课程1-13 http://www.imooc.com/video/5554 Servlet路径跳转: 绝对路径:放在任何地方都对的路径 相对路径:相对于当前资源的路径 两种方法:请求重定向,服务 ...
- Setup Spark source code environment
1. Install Java and set JAVA_HOME 2. Install Eclipse Juno Java IDE, Scala plugin and Scala Test 3. D ...
- hdu 1047 (big integer sum, fgets or scanf, make you func return useful infos) 分类: hdoj 2015-06-18 08:21 39人阅读 评论(0) 收藏
errors made, boundary conditions, <= vs < , decreasing vs increasing , ++, –, '0'/'1' vs 0/1 p ...
- 关于oracle的rowid
oracle数据库中表的每一行(元组)均有一个rowid,它是数据的详细地址,通过rowid,oracle可以快速的定位某行具体的数据的位置. ROWID可以分为物理rowid和逻辑rowid两种.普 ...
- 保留json字符串中文的函数,代替json_encode
// 格式化json中的汉字函数 protected function encode_json($str) { $strs = urldecode(json_encode($thi ...