python 基础数据类型之list:

1、列表的创建

  1. list1 = ['hello', 'world', 1997, 2000]
  2. list2 = [1, 2, 3, 4, 5 ]
  3. list3 = ["a", "b", "c", "d"]
  4. list4 = list() #创建空列表
  5. list5 = [] #创建空列表

2、访问列表中的值

列表的数据访问需要使用索引序号。 list1 = ['hello', 'world', 19, 20]

  1. list2 = [1, 2, 3, 4, 5 ]
  2. print "list1[0]: ", list1[0]
  3. print "list2[1:5]: ", list2[1:5]
  1. 输出结果:
  2. list1[0]: hello
  3. list2[1:5]: [2, 3, 4, 5]

3、数值更新

列表内容的更新可以直接使用索引序号,进行内容的更新,也可以使用append方法。

  1. list1 = ['hello', 'world', 19, 20]
  2. print list1
  3. list1[0] = "HELLO"
  4. print list1
  1. 运行结果:
  2. ['hello', 'world', 19, 20]
  3. ['HELLO', 'world', 19, 20]

4、列表元素删除

列表元素的删除使用del语句,也可以使用remove方法。

  1. list1 = ['hello', 'world', 19, 20]
  2. print list1
  3. del list1[2]
  4. print list1
  1. 运行结果:
  2. ['hello', 'world', 19, 20]
  3. ['hello', 'world', 20]

5、Python列表脚本操作符

列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。

  1. list1 = ['hello', 'world', 19, 20]
  2. print list1
  3. print list1 + list1
  4. print list1 * 3
  1. 运行结果:
  2. ['hello', 'world', 19, 20]
  3. ['hello', 'world', 19, 20, 'hello', 'world', 19, 20]
  4. ['hello', 'world', 19, 20, 'hello', 'world', 19, 20, 'hello', 'world', 19, 20]

6、列表常用的方法

list.append(obj) #在列表末尾添加新的对象

  1. list1 = ['hello', 'world', 100, 200]
  2. list1.append(300)
  3. print list1
  1. ['hello', 'world', 100, 200, 300]

running result

list.count(obj) #统计某个元素在列表中出现的次数

  1. list1 = ['hello', 'world', 100, 200, "hello"]
  2. ret1 = list1.count("hello")
  3. ret2 = list1.count(100)
  4. print ret1, ret2
  1. 2 1

running result

list.extend(seq) #在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

  1. list1 = ['hello', 'world', ]
  2. print list1
  3. list2 = [100, 200, "hello"]
  4. list1.extend(list2)
  5. print list1
  1. ['hello', 'world']
  2. ['hello', 'world', 100, 200, 'hello']

running result

list.index(obj) #从列表中找出某个值第一个匹配项的索引位置

  1. list1 = ['hello', 'world', 100, 200, "hello"]
  2. print (list1.index("world"))
  3. print (list1.index("hello")) #第一个匹配的位置
  4. print (list1.index(100))
  5. print (list1.index("")) # 找不到报错
  1. Traceback (most recent call last):
  2. 1
  3. File "C:/Users/Administrator/PycharmProject/s1/fileio.py", line 9, in <module>
  4. 0
  5. print (list1.index("")) # 找不到报错
  6. 2
  7. ValueError: '' is not in list

running result

list.insert(index, obj) #将对象插入列表

  1. list1 = ['hello', 'world', ]
  2. list2 = [100, 200, "hello", ]
  3. list1.insert(0 , "World")
  4. list1.insert(0 , list2) # 整个列表作为元素添加到原列表中
  5. print list1
  1. [[100, 200, 'hello'], 'World', 'hello', 'world']

running result

list.pop(obj=list[-1]) #移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

  1. list1 = ['hello', 'world', 100, 200, "hello", ]
  2. print list1
  3. list1.pop(0) # 移除索引为0的
  4. print list1
  5. list1.pop() # 默认从最后一个开始移除
  6. print list1
  1. ['hello', 'world', 100, 200, 'hello']
  2. ['world', 100, 200, 'hello']
  3. ['world', 100, 200]

running result

list.remove(obj) #移除列表中某个值的第一个匹配项

  1. list1 = ['hello', 'world', 100, 200, "hello", ]
  2. print list1
  3. list1.remove("hello") # 删除第一个匹配到的元素
  4. print list1
  1. ['hello', 'world', 100, 200, 'hello']
  2. ['world', 100, 200, 'hello']

running result

list.reverse() #反向列表中元素

  1. list1 = ['hello', 'world', 100, 200, "hello", ]
  2. list1.reverse()
  3. print list1
  1. ['hello', 200, 100, 'world', 'hello']

running result

list.sort([func]) #对原列表进行排序  reverse=True 反序

  1. list1 = ['hello', 'world', 100, 200, "hello", ]
  2. list2 = [1, 44, 56, 68, 34, 2, 34, 68, 1, 3, 4]
  3. list1.sort()
  4. list2.sort()
  5. print list1
  6. print list2
  1. [100, 200, 'hello', 'hello', 'world']
  2. [1, 1, 2, 3, 4, 34, 34, 44, 56, 68, 68]

running result

7、列表相关的内置函数

cmp(list1, list2) #比较两个列表的元素
len(list) #列表元素个数
max(list) #返回列表元素最大值
min(list) #返回列表元素最小值
list(seq) #将元组转换为列表

python 基础数据类型之list的更多相关文章

  1. Python基础数据类型-列表(list)和元组(tuple)和集合(set)

    Python基础数据类型-列表(list)和元组(tuple)和集合(set) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的 ...

  2. Python基础数据类型-字符串(string)

    Python基础数据类型-字符串(string) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版 ...

  3. python基础数据类型考试题

    Python基础数据类型考试题 考试时间:两个半小时                      满分100分(80分以上包含80分及格) 一,基础题. 1,简述变量命名规范(3分) 2,字节和位的关系 ...

  4. 1--Python 入门--Python基础数据类型

    一.Python基础语法 初次使用Python,首先要明确三点: Python的标识符(例如变量名.函数名等),可用字母.数字和下划线构成,不能以数字开头,且区分大小写. Python对于缩进敏感.在 ...

  5. Python基础数据类型-字典(dict)

    Python基础数据类型-字典(dict) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版本的哟 ...

  6. Python基础数据类型题

    Python基础数据类型 题考试时间:三个小时 满分100分(80分以上包含80分及格)1,简述变量命名规范(3分) 1.必须是字母,数字,下划线的任意组合. 2.不能是数字开头 3.不能是pytho ...

  7. Python基础数据类型之字符串

    Python基础数据类型之字符串 一.Python如何创建字符串 在python中用引号将一些文本包起来就构成了字符串(引号可以是单引号.双引号.单三引号,双三引号,它们是完全相同的) >> ...

  8. Python基础数据类型之集合

    Python基础数据类型之集合 集合(set)是Python基本数据类型之一,它具有天生的去重能力,即集合中的元素不能重复.集合也是无序的,且集合中的元素必须是不可变类型. 一.如何创建一个集合 #1 ...

  9. 老男孩Python==基础数据类型考试题

    转载 # Python基础数据类型考试题 # 考试时间:两个半小时 满分100分(80分以上包含80分及格) # 一,基础题. # 1, 简述变量命名规范(3分) # 1.变量由字母.数字.下划线任意 ...

随机推荐

  1. (3)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- Consul服务治理

    Consul是注册中心,服务提供者.服务提供者.服务消费者等都要注册到Consul中,这样就可以实现服务提供者.服务消费者的隔离. 除了Consul之外,还有Eureka.Zookeeper等类似软件 ...

  2. 记一次Java加密加签算法到php的坑

    此文为本人原创首发于 http://www.35coder.com/convert_encryption_codes_to_php/. 写代码的经历中,总少不了与外部的程序对接,一旦有这样的事,往往周 ...

  3. DokuWiki 使用

    新建文件夹 修改url, 将新文件夹的名称赋值给url上的id, 如要建一个"DokuWiki"的文件夹,并在文件夹下新增一个"QuickStart"的页面,改 ...

  4. Catlike学习笔记(1.1)-使用Unity实现一个钟表

    最近发现『Catlike系列教程』觉得内容真的很赞,感觉有很多地方涉及到了我的知识盲点,如果真的可以照着做下来一遍的话应该收获颇丰.因为教程很长所以逐字翻译不太可能了(主要是翻译的太差).基本上就是把 ...

  5. 自己动手写Impala UDF

    本文由  网易云发布. 概述 出于对可扩展性和性能的考虑,UDF已变成大数据生态圈查询引擎的必备功能之一,无论是Calcite.Hive.Impala都对其进行支持,但是UDF的支持有利也有弊,好处在 ...

  6. 结对&词频统计

    结对编程 Pathner 濮成林(博客:http://www.cnblogs.com/charliePU/) 1.词频统计 环境依赖: 开发环境.myeclipse 2013, jdk1.7.0_04 ...

  7. 11.12 Daily Scrum(保存草稿后忘了发布·····)

    在实现过程中,我们发现要将不同人开发的组件整合起来并不是一件容易的事,于是我们调整了一下任务,修改了一下各自的程序:   Today's tasks  Tomorrow's tasks 丁辛 餐厅列表 ...

  8. windows8/10+Ubuntu Kylin(优麒麟)双系统

    1.参考资料:http://www.jianshu.com/p/2eebd6ad284d 中第三种U盘启动方式安装完成 2.安装过程: (1)首先将一个盘空出来,做好其中数据的备份.启动win+X磁盘 ...

  9. Redis的相关问题总结

    一.Redis的优缺点及适用场景 Redis 是一个基于内存的高性能key-value数据库.很像memcached,整个数据库统统加载在内存当中进行操作,定期通过异步操作把数据库数据flush到硬盘 ...

  10. C、C ++的内存模型

    http://blog.sina.com.cn/s/blog_af9acfc60101bbcy.html