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) #将元组转换为列表
最新文章
- 《MSSQL2008技术内幕:T-SQL语言基础》读书笔记(下)
- HDU5716 : 带可选字符的多字符串匹配
- JAVA中的正则表达式
- iOS注册,找回密码时用到的获取验证码
- 使用OpenCV/python进行双目测距
- no permissions fastboot
- weblogic开发模式与生产模式介绍
- xargs 参数
- scanf(),gets(),gechar()函数小结
- error C2039: &#39;SetDefaultDllDirectories&#39;错误解决办法
- Alpha冲刺Day6
- Python正则表达式很难?一篇文章搞定他,不是我吹!
- vbs脚本实现自动打字祝福&;搞笑
- 当view为wrap_conten时获取一个view的具体宽高
- Django 简介
- JavaScript之作用域
- 机器学习技法笔记:07 Blending and Bagging
- C++异常的几种捕获方式
- Codeforces.GYM101612E.Equal Numbers(贪心)
- Hexo NexT 博客后台管理指南