Python数据基础类型-列表
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方法。insert( )在列表的任何位置添加新元素。
list1 = ['hello', 'world', 19, 20]
print list1
list1[0] = "HELLO"
print list1
list1.append(first)
print list1
list1.insert(0,'111111')
运行结果:
['hello', 'world', 19, 20]
['HELLO', 'world', 19, 20]
['HELLO', 'world', 19, 20, 'first']
['111111', 'HELLO', 'world', 19, 20, 'first']
4,列表元素删除
列表元素的删除使用del语句,也可以使用remove方法,也可使用pop()方法。
pop( )可删除末尾的元素,并让你能够接着使用它。
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,列表常用的方法
1,list.append(obj) #在列表末尾添加新的对象
list1 = ['hello', 'world', 100, 200]
list1.append(300)
print list1
['hello', 'world', 100, 200, 300]
running result
2,list.count(obj) #统计某个元素在列表中出现的次数
list1 = ['hello', 'world', 100, 200, "hello"]
ret1 = list1.count("hello")
ret2 = list1.count(100)
print ret1, ret2
2 1
3,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
4,list.index(obj) #从列表中找出某个值第一个匹配项的索引位置
list1 = ['hello', 'world', 100, 200, "hello"]
print (list1.index("world"))
print (list1.index("hello")) #第一个匹配的位置
print (list1.index(100))
print (list1.index("")) # 找不到报错
1
0
2
Traceback (most recent call last):
File "D:/PyCharm_Code/extend.py", line 5, in <module>
print (list1.index("")) # 找不到报错
ValueError: '' is not in list
running result
5,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
6,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
7,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
8,list.reverse() #反向列表中元素
list1 = ['hello', 'world', 100, 200 ]
list1.reverse()
print(list1)
[200, 100, 'world', 'hello']
running result
9,使用方法sort( )对列表进行永久性排序,reverse=True 倒序排序
cars = ['bmw','audi','toyota','subaru']
cars.sort()
print(cars)
cars.sort(reverse=True)
print(cars)
['audi', 'bmw', 'subaru', 'toyota']
['toyota', 'subaru', 'bmw', 'audi']
running result
10,使用函数sorted( )对列表进行临时排序,reverse=True 倒序排序
cars = ['bwm','audi','toyota','subaru']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print("\nHere is the original list again:")
print(cars)
Here is the original list:
['bwm', 'audi', 'toyota', 'subaru'] Here is the sorted list:
['audi', 'bwm', 'subaru', 'toyota'] Here is the original list again:
['bwm', 'audi', 'toyota', 'subaru']
running result
7、列表相关的内置函数
cmp(list1, list2) #比较两个列表的元素
len(list) #列表元素个数
max(list) #返回列表元素最大值
min(list) #返回列表元素最小值
list(seq) #将元组转换为列表
Python数据基础类型-列表的更多相关文章
- Python数据基础类型-新建列表
1,遍历列表 遍历列表的所有元素,对每个元素执行相同的操作.可使用for循环 magicians = ['alice','david','carolina'] for magician in magi ...
- 2--Python入门--Python数据集合类型--列表
在基础数据类型的基础上,Python有6中数据集合的类型: 列表list,最常用的数据类型,以[]为标识 元组tuple,和list很相似,但是不能二次赋值,用()标识 集合set,和list类似,但 ...
- Python的基础类型(int,bool,str):
Python的基础类型(int,bool,str): 1.int -------> 整形:主要用力进行数字计算 2.string ------>字符串:可以保存少量数据并进行相关的操作 3 ...
- Python数据基础--列表、元组、字典、函数
一.数据结构 列表(List)和元组 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内 ...
- python基础类型—列表
列表 列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如: li = [‘alex’ ...
- python笔记---数据基础类型
s = 'laonanHai' s1 = s.capitalize() #首字母大写,其他字母小写 s2 = s.upper() #全部大写 s3 = s.lower() #全部小写 print(s, ...
- 4--Python入门--Python数据集合类型--集合
在基础数据类型的基础上,Python有6中数据集合的类型: 列表list,最常用的数据类型,以[]为标识 元组tuple,和list很相似,但是不能二次赋值,用()标识 集合set,和list类似,但 ...
- 3--Python入门--Python数据集合类型--元组
在基础数据类型的基础上,Python有6中数据集合的类型: 列表list,最常用的数据类型,以[]为标识 元组tuple,和list很相似,但是不能二次赋值,用()标识 集合set,和list类似,但 ...
- Python入门基础学习(列表/元组/字典/集合)
Python基础学习笔记(二) 列表list---[ ](打了激素的数组,可以放入混合类型) list1 = [1,2,'请多指教',0.5] 公共的功能: len(list1) #/获取元素 lis ...
随机推荐
- [CSP-S模拟测试]:真相(模拟)
题目传送门(内部题106) 输入格式 第一行为一个正整数$T$,表示数据组数. 接下来$T$组数据,每组数据第一行一个正整数$n$表示$OIer$,接下来$n$行,第$i$行表示编号为$i$的人所说的 ...
- springboot 项目中在普通类中调用dao层的mapper 出现空指针异常
项目中我遇到同样的问题 特记载一下 有两种方式 一. 该类使用@Component注解 添加一个本类类型的静态字段 创建一个初始化方法,贴上@PostConstruct 标签,用于注入bean 创建方 ...
- LeetCode 54. 螺旋矩阵(Spiral Matrix) 剑指offer-顺时针打印矩阵
题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, ...
- Python中的super()用法
Python中对象方法的定义很怪异,第一个参数一般都命名为self(相当于其它语言的this,比如:C#),用于传递对象本身,而在调用的时候则不 必显式传递,系统会自动传递. 今天我们介绍的主角是su ...
- XAMPP安装后启动Apache的Busy解决方法
启动apache后,一直提示80 busy 使用netstat -ano查看,并无端口占用,真是奇怪. 百度之后发现有可能是启动后,ssl端口占用导致. XAMPP默认会加载一个SSL模块,它要占用一 ...
- ControlTemplate in WPF —— Checkbox
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- Selenium 2自动化测试实战1(1-2章节重点笔记)
1.黑盒测试 黑盒测试,指的是把被测的软件看做一个黑盒子,不去关心盒子里面的结构是什么样子的,只关心软件的输入数据和输出结果. 2.白盒测试白盒测试,指的是把盒子打开,去研究里面的源代码和程序执行结果 ...
- 阶段3 2.Spring_07.银行转账案例_7 代理的分析
新建项目 实现动态代理. 动态代理的概念 买电脑找代理商 代理的出现 解决了生产厂家的一些问题 需要java中的动态代理机制
- JavaScript 积累
1. 基本类型值在内存中占据固定大小的空间,因此被保存在栈空间中: 2. 引用类型的值是对象,保存在堆空间中: 3. 从一个变量向另一个变量复制基本类型的值,会创建这个值的一个副本:从一个变量向另一个 ...
- jmeter响应数据Unicode编码转换为汉字
2018-07-09 10:24:34 每次用jmeter做接口测试时,响应信息中文总是显示Unicode编码格式,每次都要在网上寻找这一段转换的代码,但是我发现在网上找这段代码有点麻烦,像我 ...