|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
• 列表可以存放:字符串,整型,浮点型,对象
• 创建列表
○ 创建一个普通列表 num = [1,2,3,4,5]
○ 创建一个混合列表 mix = [1, 3.43,“你好”, [1,2,3]]
○ 创建一个空列表 empty = []
• 向列表添加元素
○ list.append()只能添加一个准备插入列表的元素
>>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> list.append("new")
>>> list
['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'new']
○ list.extend()只能添加一个列表用于扩展列表,列表中可以有多个元素 >>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> list.extend(["new1",12,"new2"])
>>> list
['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'new1', 12, 'new2']
○ list.insert()有两个参数,一个为位置索引值,一个为插入列表的元素,注意索引值从0开始 >>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> list.insert(3,"new")
>>> list
['red', 'orange', 'yellow', 'new', 'green', 'blue', 'purple']
• 从列表中获取元素
○ python列表支持正数与负数索引
○ list[位置索引]来获取列表元素 >>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> list[0]
'red'
#列表中的列表用附加索引值来获取
>>> list = ["red", "orange", ["yellow", "green", "blue"], "purple"]
>>> list[2][0]
'yellow'
○ list1[0] 返回第0个元素的值,list1[0:1] 返回一个只含有第0个元素的列表
>>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> list [0]
'red'
>>> list [0:1]
['red']
• 从列表中删除元素
○ list.remove()不需要知道元素在哪个位置
>>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> list.remove("red")
>>> list
['orange', 'yellow', 'green', 'blue', 'purple']
○ del list[位置索引] 可删除元素,也可删除列表
>>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> del list[0]
>>> list
['orange', 'yellow', 'green', 'blue', 'purple']
#“del”语句可删除元素,也可删除列表
>>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> del list
○ list.pop()剔除列表中最后一个元素,并显示出来
>>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> list.pop()
'purple'
>>> list
['red', 'orange', 'yellow', 'green', 'blue']
#可给pop加上一个索引值,指定删除与显示的元素
>>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> list.pop(2)
'yellow'
>>> list
['red', 'orange', 'green', 'blue', 'purple']
• 列表分片
○ 利用列表分片,我们可以一次性从列表获取多个元素,且不改变原列表
>>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> list[1:3]
['orange', 'yellow']
#如果想从第一个数开始,并不需要写零,如果想在最后一个数结束,同样不需要
#[:]进行深拷贝可以在不影响原数列的前提下,对拷贝出来的数列进行调整
>>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> list[:4]
['red', 'orange', 'yellow', 'green']
>>> list[2:]
['yellow', 'green', 'blue', 'purple']
>>> list[:]
['red', 'orange', 'yellow', 'green', 'blue', 'purple']
#在普通的分片操作中,步长默认设置为1,表示逐个遍历元素,也可更改步长
#步长不能为0
补偿可以是负数,改变方向
>>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> list[::2]
['red', 'yellow', 'blue']
>>> list[1::2]
['orange', 'green', 'purple']
>>> list[::-2]
['purple', 'green', 'orange']
• 列表常用操作符
○ 比较操作符:比较数据行列表的元素,只比较列表中第一个可以比较的元素
>>> list1 = [123,456]
>>> list2 = [456,0]
>>> list2 > list1
True
○ 逻辑操作符:and等可以进行列表之间的逻辑判断
○ 连接操作符:“+”可以实现拼接,但不推荐此法
○ 重复操作符:for
list = ["red", "orange", "yellow", "green", "blue", "purple"]
for each in list:
print(each)
○ 成员关系操作符
>>> list = ["red", "orange", "yellow", "green", "blue", "purple"]
>>> "red" in list
True
>>> "red" not in list
False
#调用列表里的列表的元素需要加上索引位置
>>> list = ["red", "orange", ["yellow", "green", "blue"], "purple"]
>>> "yellow" in list
False
>>> "yellow" in list[2]
True
• 列表的内置函数BIF
○ list.count(A)输出参数A在列表中出现的次数
○ list.index(A)输出参数A在列表中的位置
§ 如在列表中多次出现,取第一个出现的参数的位置
§ 也可寻找列表中范围内的参数的位置
□ list.index(A,3(范围起始位置),7(范围结束位置))
○ list.reverse()前后翻转参数在列表中的位置(不需要参数)
○ list.sort()用指定的方式对列表中的参数进行排列
>>> list = [3,12,5,79,0,32]
>>> list.sort()
>>> list
[0, 3, 5, 12, 32, 79]
#list.sort(function, key)
#如list.sort(reverse=True)从大到小排序
>>> list = [3,12,5,79,0,32]
>>> list.sort(reverse=True)
>>> list
[79, 32, 12, 5, 3, 0]
○ list.copy()方法跟使用切片拷贝是一样的
list.clear()用于清空列表的元素,使列表变成一个空列表 |
|