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学习笔记七
面向对象编程 面向对象的特性如下: 类:具有相同属性和方法的一类事物,成为类. 对象:类的实例化后的结果,一个类可以实例化多个对象,每个对象也可以不同的属性. 封装:在类中对数据的赋值,类里面包含着类 ...
随机推荐
- 3dContactPointAnnotationTool开发日志(四)
没办法,为了能在寝室接着做这玩意只好又在电脑上装一个和实验室版本一样的unity了.虽然打开后UI界面还是一团糟,不过至少要的东西都在,又手动调了调UI界面. 然后把旋转视角功能加上了.鼠标右 ...
- Linux下安装MySQL管理工具MySQL Administrator和MySQL Query Browser(转载)
文章来源:http://blog.csdn.net/sunrier/article/details/7572299 Linux下MySQL Administrator和MySQL Query Brow ...
- Python对字符串进行MD5加密处理
import hashlibimport sysreload(sys)sys.setdefaultencoding('utf-8') m = hashlib.md5()m.update('123456 ...
- c运行时库,c标准库,Windows系统api的关系
原文地址:http://blog.csdn.net/seastars_sh/article/details/8233324 C运行库和C标准库的关系 C标准库,顾名思义既然是标准,就是由标准组织制定的 ...
- 【转】自编码算法与稀疏性(AutoEncoder and Sparsity)
目前为止,我们已经讨论了神经网络在有监督学习中的应用.在有监督学习中,训练样本时有类别标签的.现在假设我们只有一个没带类别标签的训练样本集合 ,其中 .自编码神经网络是一种无监督学习算法,它使用了 ...
- 【bzoj1212】[HNOI2004]L语言 AC自动机
题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...
- [洛谷P4777]【模板】扩展中国剩余定理(EXCRT)
题目大意:给你一些关于$x$的方程组:$$\begin{cases}x\equiv a_1\pmod{mod_1}\\x\equiv a_2\pmod{mod_2}\\\vdots\\x\equiv ...
- 【hackerrank】Week of Code 26
在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...
- P2805 [NOI2009]植物大战僵尸(最小割+拓扑排序)
题意: n*m的矩阵,每个位置都有一个植物.每个植物都有一个价值(可以为负),以及一些它可以攻击的位置.从每行的最右面开始放置僵尸,僵尸从右往左行动,当僵尸在植物攻击范围内时会立刻死亡.僵尸每到一个位 ...
- BZOJ5312:冒险——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5312 Kaiser终于成为冒险协会的一员,这次冒险协会派他去冒险,他来到一处古墓,却被大门上的守护 ...