python 基础数据类型之list:

1、列表的创建

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

2、访问列表中的值

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

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

3、数值更新

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

list1 = ['hello', 'world', 19, 20]
print list1
list1[0] = "HELLO"
print list1
运行结果:
['hello', 'world', 19, 20]
['HELLO', 'world', 19, 20]

4、列表元素删除

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

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

5、Python列表脚本操作符

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

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

6、列表常用的方法

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

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

running result

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

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

running result

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

list1 = ['hello', 'world', ]
print list1
list2 = [100, 200, "hello"]
list1.extend(list2)
print list1
['hello', 'world']
['hello', 'world', 100, 200, 'hello']

running result

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

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

running result

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

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

running result

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

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

running result

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

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

running result

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

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

running result

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

list1 = ['hello', 'world', 100, 200, "hello", ]
list2 = [1, 44, 56, 68, 34, 2, 34, 68, 1, 3, 4]
list1.sort()
list2.sort()
print list1
print list2
[100, 200, 'hello', 'hello', 'world']
[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. python 回溯法 记录

    一直不是太理解回溯法,这几天集中学习了一下,记录如下. 回溯法有"通用的解题法"之称. 1.定义:  也叫试探法,它是一种系统地搜索问题的解的方法. 2.基本思想:  从一条路往前 ...

  2. Windows下的Anaconda+OpenCV的环境配置

    Windows下的Anaconda+OpenCV的环境配置

  3. DeepFM算法解析及Python实现

    1. DeepFM算法的提出 由于DeepFM算法有效的结合了因子分解机与神经网络在特征学习中的优点:同时提取到低阶组合特征与高阶组合特征,所以越来越被广泛使用. 在DeepFM中,FM算法负责对一阶 ...

  4. Linux 入门记录:十二、Linux 权限机制

    一.权限 权限是操作系统用来限制资源访问的机制,权限一般分为读.写.执行. 系统中每个文件都拥有特定的权限.所属用户及所属组,通过这样的机制来限制哪些用户.哪些组可以对特定的文件进行什么样的操作. 每 ...

  5. 软件工程第三次作业(One who wants to wear the crown, Bears the crown.)

    最大连续子数组和 题目 给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+a[i+1]+-+a[j]的子段和的最大值.当所给的整数均为负数时定义子段和 ...

  6. BugkuCTF 文件上传测试

    前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...

  7. LABVIEW串口通信基础

    写这一篇串口通信基础的契机是最近刚刚完成一个温箱的仪器控制程序,LABVIEW通过串口与温箱单片机通讯,我打算将过程中遇到的一些问题和收获列在这里方便有需求的网友比对.寻找答案. 学LABVIEW时间 ...

  8. RabbitMQ基础教程之基本使用篇

    RabbitMQ基础教程之基本使用篇 最近因为工作原因使用到RabbitMQ,之前也接触过其他的mq消息中间件,从实际使用感觉来看,却不太一样,正好趁着周末,可以好好看一下RabbitMQ的相关知识点 ...

  9. Java收发邮件过程中具体的功能是怎么实现的

    SMTP协议 用户连上邮件服务器后,要想给它发送一封电子邮件,需要遵循一定的通迅规则,SMTP协议就是用于定义这种通讯规则的. 因而,通常我们也把处理用户smtp请求(邮件发送请求)的邮件服务器称之为 ...

  10. 【翻译】Brewer's CAP Theorem CAP定理

    Brewer's CAP Theorem 原文地址:http://www.julianbrowne.com/article/brewers-cap-theorem Brewer’s (CAP) The ...