python学习笔记(七)之列表
列表:是一个加强版的数组,什么东西都可以往里面放。
创建列表
创建一个普通列表:
>>> member = ['operating system', 'data structure', 'network', 'principle of computer composition']
>>> member
['operating system', 'data structure', 'network', 'principle of computer composition']
创建一个混合列表:
>>> mix = [1, 'hello python', 3.14159, [1,2,3,['one','two']]]
>>> mix
[1, 'hello python', 3.14159, [1, 2, 3, ['one', 'two']]]
创建一个空列表:
>>> empty = []
>>> empty
[]
向列表中添加元素
使用append(),extend(),和insert()方法
append()在尾部添加,extend()用一个列表来扩展列表,insert(index, obj)在index处插值。
>>> member.append('mechine learning')
>>> member
['operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning']
>>> member.extend(['data mining', 'hadoop'])
>>> member
['operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']
>>> member.insert(0,'PE')
>>> member
['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']
从列表中获取元素
和数组一样,通过元素的索引从列表中获得单个元素,索引从0开始。
>>> for i in range(len(member)):
... print(member[i])
...
PE
operating system
data structure
network
principle of computer composition
mechine learning
data mining
hadoop
通过列表分片
>>> member[3]
'network'
>>> member[:3]
['PE', 'operating system', 'data structure']
>>> member[3:]
['network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']
>>> member[3:5]
['network', 'principle of computer composition']
从列表中删除元素
使用remove(),pop()方法或del
>>> member
['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']
>>> member.remove('hadoop')
>>> member
['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining']
>>> member.pop()
'data mining'
>>> member
['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning']
>>> member.pop(2)
'data structure'
>>> member
['PE', 'operating system', 'network', 'principle of computer composition', 'mechine learning']
>>> del member[3]
>>> member
['PE', 'operating system', 'network', 'mechine learning']
>>> del member
>>> member
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'member' is not defined
列表的一些常用操作符
比较操作符 >, >=, < ,<=, == ,!=
>>> list1 = [123]
>>> list2 = [456]
>>> list1 < list2
True
>>> list1 = [123,456]
>>> list2 = [234,123]
>>> list1 > list2
False
逻辑操作符 and, or, not
>>> list3 = [123,456]
>>> list1 < list2 and list1 == list3
True
>>> list1 > list2 or list1 < list2
True
>>> not list1 < list2
False
拼接操作符 *
>>> list4 = list2 + list3
>>> list4
[234, 123, 123, 456]
重复操作符 *
>>> list4
[234, 123, 123, 456]
>>> list4 * 5
[234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456]
成员关系操作符in, not in
>>> 123 in list4
True
>>> 345 in list4
False
>>> 345 not in list4
True
列表一些常用的方法
count():返回列表内某个成员出现次数,如果不存在则返回0
>>> list4
[234, 123, 123, 456]
>>> list4.count(123)
2
index():返回该元素在列表第一次出现位置的索引,若不存在,则抛出一个ValueError异常。
>>> list4.index(123)
1
>>> list4.index(124)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 124 is not in list
reverse():翻转列表
>>> list4
[456, 123, 123, 234]
>>> list4.reverse()
>>> list4
[234, 123, 123, 456]
python学习笔记(七)之列表的更多相关文章
- python学习笔记(一)、列表和元祖
该一系列python学习笔记都是根据<Python基础教程(第3版)>内容所记录整理的 1.通用的序列操作 有几种操作适用于所有序列,包括索引.切片.相加.相乘和成员资格检查.另外,Pyt ...
- Python学习笔记 (3) :列表、元组的操作
列表,即写在方括号之间.用逗号分隔开的数值列表.列表内的项目不必全是相同的类型. >>> a = ['spam', 'eggs', 100, 1234] >>> a ...
- python 学习笔记二 (列表推导式)
2018年年初写了第一篇博客,说要做一个认真的技术人 https://www.cnblogs.com/yingchen/p/8455507.html 今天已经是11月19日了,这是第二篇博客,看来坚持 ...
- Python学习笔记 第一课 列表
Python的列表就像是一个数组: 一.创建列表 movies=["The Holy Grail","Then Life of Brian","The ...
- python学习笔记4(列表)
列表是最通用的Python复合数据类型,列表中包含以逗号分隔,并在方括号([])包含的项目. 在一定程度上,列表相似C语言中的数组,它们之间的一个区别是,所有属于一个列表中的项目可以是不同的数据类型的 ...
- Python学习笔记七-错误和异常
程序员总是和各种错误打交道,学习如何识别并正确的处理程序错误是很有必要的. 7.1错误和异常 1.错误 从软件方面来看,错误分为语法错误和逻辑错误两种.这两种错误都将导致程序无法正常进行下去,当Pyt ...
- Python学习笔记6(列表生成式)
1.生成列表 要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],我们可以用range(1, 11): >>> range(1, 11) [1, 2, 3 ...
- python学习笔记之一:列表与元组
最近在看<python基础教程>(基于python2.x),下面总结一下第二章列表与元组的知识: 在这章中引入了数据结构的概念.数据结构是通过某种方式组织在一起的数据元素的集合.在pyth ...
- 【python学习笔记】2.列表和元组
# 第二章:列表和元组 序列中,每个元素都有个一个序号,序号以0开始,最后一个元素序号为-1,倒数第二个-2 序列类型包括,列表,元组,字符串,unicode字符串,buffer, xrange ...
- Python学习笔记七
面向对象编程 面向对象的特性如下: 类:具有相同属性和方法的一类事物,成为类. 对象:类的实例化后的结果,一个类可以实例化多个对象,每个对象也可以不同的属性. 封装:在类中对数据的赋值,类里面包含着类 ...
随机推荐
- [zt]手把手教你写对拍程序(PASCAL)
谁适合看这篇文章? ACMERS,OIERS或其它参加算法竞赛或需要算法的人 对操作系统并不太熟悉的人 不会写对拍的人 在网上找不到一个特别详细的对拍样例的人 不嫌弃我写的太低幼的人 前言 在NOIP ...
- Windows API封装:LoadLibrary/FreeLibrary
LoadLibrary/LoadLibraryEx用来加载DLL到自己的进程空间,使用完用FreeLibrary释放,一般使用方式如下: HINSTANCE hInstRich = ::Load ...
- [剑指Offer] 50.数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- VBA 实现学校上课教员一学期中所有上课时间,在一页中通过背景底色反应出来
需求:学校一学期的所有课程表,每个教员都有可能上好几门课,但给一个教员调课时需要查找所调课时间位置有没有此教员上其它的课 相冲突,手动查找很不方便,这里想通过一个表中位置显示出同一教员在所有课表中出现 ...
- BZOJ 1010 玩具装箱(斜率优化DP)
dp[i]=min(dp[j]+(sum[i]-sum[j]+i-j-1-L)^2) (j<i) 令f[i]=sum[i]+i,c=1+l 则dp[i]=min(dp[j]+(f[i]-f[j] ...
- 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路
题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...
- 【bzoj3772】精神污染 STL+LCA+主席树
题目描述 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位是森林和山地,与拥有关西机场的大阪府比邻而居,是关西地区面积最大的县,是集经济和文化于一体的一大地区,是日本西部门户 ...
- 【bzoj1901】Zju2112 Dynamic Rankings 离散化+主席树+树状数组
题目描述 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是多少(1≤k≤ ...
- kaptcha验证码在windows下正常,在linux下无法显示
有几种情况,记录备忘: 1.两个环境字体不一样,linux环境下可能没有字体,重新安装字体即可. 2.tomcat等容器下没有temp目录,手动建立即可. 3.如果报找不到类的错误,检查JDK是否正确 ...
- 【题解】SCOI2006萌萌哒
看到这题,首先想到\(n^{2}\)的暴力,就是用并查集暴力合并两个相等的点.但由于这样会导致反复地访问同一个操作,显然是不能够的.于是我们可以联想这题的特殊性质,就是互相连变的点都是一段一段的区间. ...