列表:是一个加强版的数组,什么东西都可以往里面放。

创建列表

创建一个普通列表:

  1. >>> member = ['operating system', 'data structure', 'network', 'principle of computer composition']
  2. >>> member
  3. ['operating system', 'data structure', 'network', 'principle of computer composition']

创建一个混合列表:

  1. >>> mix = [1, 'hello python', 3.14159, [1,2,3,['one','two']]]
  2. >>> mix
  3. [1, 'hello python', 3.14159, [1, 2, 3, ['one', 'two']]]

创建一个空列表:

  1. >>> empty = []
  2. >>> empty
  3. []

向列表中添加元素

使用append(),extend(),和insert()方法

append()在尾部添加,extend()用一个列表来扩展列表,insert(index, obj)在index处插值。

  1. >>> member.append('mechine learning')
  2. >>> member
  3. ['operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning']
  4. >>> member.extend(['data mining', 'hadoop'])
  5. >>> member
  6. ['operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']
  7. >>> member.insert(0,'PE')
  8. >>> member
  9. ['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']

从列表中获取元素

和数组一样,通过元素的索引从列表中获得单个元素,索引从0开始。

  1. >>> for i in range(len(member)):
  2. ... print(member[i])
  3. ...
  4. PE
  5. operating system
  6. data structure
  7. network
  8. principle of computer composition
  9. mechine learning
  10. data mining
  11. hadoop

通过列表分片

  1. >>> member[3]
  2. 'network'
  3. >>> member[:3]
  4. ['PE', 'operating system', 'data structure']
  5. >>> member[3:]
  6. ['network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']
  7. >>> member[3:5]
  8. ['network', 'principle of computer composition']

从列表中删除元素

使用remove(),pop()方法或del

  1. >>> member
  2. ['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']
  3. >>> member.remove('hadoop')
  4. >>> member
  5. ['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining']
  6. >>> member.pop()
  7. 'data mining'
  8. >>> member
  9. ['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning']
  10. >>> member.pop(2)
  11. 'data structure'
  12. >>> member
  13. ['PE', 'operating system', 'network', 'principle of computer composition', 'mechine learning']
  14. >>> del member[3]
  15. >>> member
  16. ['PE', 'operating system', 'network', 'mechine learning']
  17. >>> del member
  18. >>> member
  19. Traceback (most recent call last):
  20. File "<stdin>", line 1, in <module>
  21. NameError: name 'member' is not defined

列表的一些常用操作符

  比较操作符 >, >=, < ,<=, == ,!=

  1. >>> list1 = [123]
  2. >>> list2 = [456]
  3. >>> list1 < list2
  4. True
  5. >>> list1 = [123,456]
  6. >>> list2 = [234,123]
  7. >>> list1 > list2
  8. False

  逻辑操作符 and, or, not

  1. >>> list3 = [123,456]
  2. >>> list1 < list2 and list1 == list3
  3. True
  4. >>> list1 > list2 or list1 < list2
  5. True
  6. >>> not list1 < list2
  7. False

  拼接操作符 *

  1. >>> list4 = list2 + list3
  2. >>> list4
  3. [234, 123, 123, 456]

  重复操作符 *

  1. >>> list4
  2. [234, 123, 123, 456]
  3. >>> list4 * 5
  4. [234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456]

  成员关系操作符in, not in

  1. >>> 123 in list4
  2. True
  3. >>> 345 in list4
  4. False
  5. >>> 345 not in list4
  6. True

列表一些常用的方法

  count():返回列表内某个成员出现次数,如果不存在则返回0

  1. >>> list4
  2. [234, 123, 123, 456]
  3. >>> list4.count(123)
  4. 2

  index():返回该元素在列表第一次出现位置的索引,若不存在,则抛出一个ValueError异常。

  1. >>> list4.index(123)
  2. 1
  3. >>> list4.index(124)
  4. Traceback (most recent call last):
  5. File "<stdin>", line 1, in <module>
  6. ValueError: 124 is not in list

  reverse():翻转列表

  1. >>> list4
  2. [456, 123, 123, 234]
  3. >>> list4.reverse()
  4. >>> list4
  5. [234, 123, 123, 456]

python学习笔记(七)之列表的更多相关文章

  1. python学习笔记(一)、列表和元祖

    该一系列python学习笔记都是根据<Python基础教程(第3版)>内容所记录整理的 1.通用的序列操作 有几种操作适用于所有序列,包括索引.切片.相加.相乘和成员资格检查.另外,Pyt ...

  2. Python学习笔记 (3) :列表、元组的操作

    列表,即写在方括号之间.用逗号分隔开的数值列表.列表内的项目不必全是相同的类型. >>> a = ['spam', 'eggs', 100, 1234] >>> a ...

  3. python 学习笔记二 (列表推导式)

    2018年年初写了第一篇博客,说要做一个认真的技术人 https://www.cnblogs.com/yingchen/p/8455507.html 今天已经是11月19日了,这是第二篇博客,看来坚持 ...

  4. Python学习笔记 第一课 列表

    Python的列表就像是一个数组: 一.创建列表 movies=["The Holy Grail","Then Life of Brian","The ...

  5. python学习笔记4(列表)

    列表是最通用的Python复合数据类型,列表中包含以逗号分隔,并在方括号([])包含的项目. 在一定程度上,列表相似C语言中的数组,它们之间的一个区别是,所有属于一个列表中的项目可以是不同的数据类型的 ...

  6. Python学习笔记七-错误和异常

    程序员总是和各种错误打交道,学习如何识别并正确的处理程序错误是很有必要的. 7.1错误和异常 1.错误 从软件方面来看,错误分为语法错误和逻辑错误两种.这两种错误都将导致程序无法正常进行下去,当Pyt ...

  7. Python学习笔记6(列表生成式)

    1.生成列表 要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],我们可以用range(1, 11): >>> range(1, 11) [1, 2, 3 ...

  8. python学习笔记之一:列表与元组

    最近在看<python基础教程>(基于python2.x),下面总结一下第二章列表与元组的知识: 在这章中引入了数据结构的概念.数据结构是通过某种方式组织在一起的数据元素的集合.在pyth ...

  9. 【python学习笔记】2.列表和元组

    # 第二章:列表和元组   序列中,每个元素都有个一个序号,序号以0开始,最后一个元素序号为-1,倒数第二个-2 序列类型包括,列表,元组,字符串,unicode字符串,buffer, xrange ...

  10. Python学习笔记七

    面向对象编程 面向对象的特性如下: 类:具有相同属性和方法的一类事物,成为类. 对象:类的实例化后的结果,一个类可以实例化多个对象,每个对象也可以不同的属性. 封装:在类中对数据的赋值,类里面包含着类 ...

随机推荐

  1. java设计模式简介

    设计模式简介: 设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用.设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案.这些解决方案是众多 ...

  2. 全排列 next_permutation() 函数的用法

    在头文件<algorithm>里面有如下代码: int a[]; do { } while(next_permutation(a,a+n)); 可产生1~n的全排列有如下代码: #incl ...

  3. 第三章 持续集成jenkins工具使用之邮件配置

    1   Email Extension Plugin插件安装 持续集成很重要的一环就是及时将构建结果通知到对应的责任人,如:构建失败了,至少需要下发通知给造成本次构建失败的开发人员,如果包含自动化测试 ...

  4. 【转】how can i build fast

    http://blog.csdn.net/pcliuguangtao/article/details/5830860

  5. Java基础之开关语句详解

    switch 语句是单条件多分支的开关语句,它的一般格式定义如下(其中break语句是可选的): switch(表达式) { case 常量值: 若干个语句 break; case  常量值: 若干个 ...

  6. 前端基础:JavaScript介绍

    JavaScript介绍 一.JavaScript简介 1.在1995年时,由Netscape公司的Brendan Eich,在网景导航者浏览器上首次设计实现二层,因为Netscape与Sun合作,N ...

  7. Windows7上安装Git

    我首先是百度到了这个网站:https://git-scm.com/download/win 这个网站上有下载链接,你可以根据你的系统选择不同的下载链接,我的是Win7 x64位的,下载地址为: htt ...

  8. Codeforces 854C Planning(贪心+堆)

    贪心:让代价大的尽量移到靠前的位置. 做法:先让前k个数加进堆里,枚举k+1~n+k,每次把新元素加进堆后找到最大代价放在当前位置即可. #include<bits/stdc++.h> # ...

  9. ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别【转】

    原文地址:https://blog.csdn.net/py_xin/article/details/52052627 ContextLoaderListener和DispatcherServlet都会 ...

  10. Codeforces Round #342 (Div. 2) C

    C. K-special Tables time limit per test 2 seconds memory limit per test 256 megabytes input standard ...