"""author = 陈俊龙"""
=================认识列表=============
1.什么是列表(list)
列表是容器型数据类型(序列),可变(支持元素的增,删,改)并且有序(支持下标操作)
将[]作为容器,容器中元素用逗号隔开
list1 = [34, 78, 90, 85, 33]
list1 = ['小明', '校长', '小李', 33]
2.列表中的元素
1)python中任何类型的数据都可以作为列表的元素,例如:数字,字符串,列表,元组,字典,函数,自定义类型等
2)同一个列表中的元素类型可以不同
list1 = [1,'xiaoming',True,[1,2],{'a':1}]
3.数据的增删改查:
查 -- 获取列表中的元素
1)获取单个元素:
语法:
列表[下标] (下标的表示方式和字符串中的相同)
names = ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋']
print(names[1])
2)获取部分元素(切片):
语法:
列表[开始下标:结束下标:步长] (切片的语法和字符串切片语法相同)
names = ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋']
print(names[1::2]) # ['张学友', '陈奕迅', '邓紫棋']
print(names[1:5:2]) # ['张学友', '陈奕迅']
print(names[:]) # ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋']
print(names[::-1]) # ['邓紫棋', '邓丽君', '陈奕迅', '刘德华', '张学友', '蔡徐坤']
3)遍历
直接遍历列表来获取其中元素
names = ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋']
for item in names:
print(item)
for item in names[::-1]:
print(item)
通过遍历下标来获取其中元素
for index in range(len(names)):
print(names[index])
for index in range(-1, -len(names)-1, -1):
print(names[index])
=================增删改==============
增删改对应的操作都会修改原列表,不会产生新列表
1.增(指的是在列表中添加元素)
1)列表.append(元素) - 在列表的最后添加一个元素
names = ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋']
names.append('王力宏')
print(names) # ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋', '王力宏']
2)列表.insert(下标,元素)- 在列表指定的下标!前!插入指定元素
names = ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋', '王力宏']
names.insert(0, '周杰伦')
print(names) # ['周杰伦', '蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋', '王力宏']
2.删(删除元素)
1)del 列表[下标] - 删除列表中指定下标对应的元素
names = ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋', '王力宏']
del names[0]
print(names) # ['张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋', '王力宏']
2)列表.remove(元素) - 删除列表中第一个指定下标(注意!只删第一个出现的元素)
names = ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋', '王力宏']
names.remove('刘德华')
print(names) # ['蔡徐坤', '张学友', '陈奕迅', '邓丽君', '邓紫棋', '王力宏']
3)列表.pop() - 取出列表中最后一个元素(注意是取出!所以会返回取出来的数据,但列表中依旧没有这个元素了)
列表.pop(下标) - 取出列表中指定下标对应的元素
names = ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋', '王力宏']
name = names.pop()
print(name) # 王力宏
print(names) # ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋']
3.改(修改列表中的某个元素的值)
列表[下标] = 新值 - 将列表中指定下标对应的元素设置为新值
names = ['蔡徐坤', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋', '王力宏']
names[0] = '周杰伦'
print(names) # ['周杰伦', '张学友', '刘德华', '陈奕迅', '邓丽君', '邓紫棋', '王力宏']
# 练习:将分数在60以下的删除
```python
scores = [89, 59, 43, 100, 98, 30, 70, 45, 57, 88]
jixu = True
while jixu:
for num in scores:
if num < 60:
scores.remove(num)
break
else:
jixu = False
print(scores)
#方法2:
scores = [89, 59, 43, 100, 98, 30, 70, 45, 57, 88]
index = 0
while index < len(scores):
if scores[index] < 60:
del scores[index]
continue
index += 1
print(scores)
# 方法3:
scores = [89, 59, 43, 100, 98, 30, 70, 45, 57, 88]
tem = scores[:]
for score in tem:
if score < 60:
scores.remove(score)
print(scores)
===============列表相关运算===========
1.数学运算:+ ,*
1)列表1 + 列表2 - 将两个列表中的元素合并产生一个新列表
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
print(list1 + list2) # [1, 2, 3, 'a', 'b', 'c']
2) 列表1 * N - 将列表中的元素重复N次产生一个新的列表
print(list1 * 3) # [1, 2, 3, 1, 2, 3, 1, 2, 3]
2.比较运算:==, !=!
3.in 和not in 操作 - 判断列表是否包含或不包含指定元素
元素 in 列表
list1 = [1, 2, 3]
print(1 in list1) #True
4.len(序列) -- 获取序列中元素的个数
5.list(数据) -- 将数据转换成列表 注意!必须是序列
哪些类型可以转换:
所以的序列都可以转换成列表
#如何转换:将序列的元素作为列表元素
list3 = list('hello')
print(list3) # ['h', 'e', 'l', 'l', 'o']
============列表相关方法============
1.内置函数:
max(序列),min(序列),sum(序列)
注意:
max(序列),min(序列) -- 序列中元素类型必须相同,序列中元素必须支持比较运算符
sum(序列) -- 只支持数字序列的求和
scores = [23, 34, 45, 56, 67, 78, 89]
print(max(scores))
print(min(scores))
print(sum(scores))
2.列表相关的方法
list.count(元素) -- 统计某个元素(obj)在列表中出现的次数
list.extend(序列) -- 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
scores.extend([20, 30])
print(scores) # [23, 34, 45, 56, 67, 78, 89, 20, 30]
list.index(元素) -- 从列表中找出某个元素!第一个!匹配项的索引位置
print(scores.index(23)) # 0
list.reverse() -- 反向列表中元素
scores.reverse()
print(scores) # [30, 20, 89, 78, 67, 56, 45, 34, 23]
list.sort( key=None, reverse=False) -- 对原列表进行排序(要求元素类型相同,元素支持比较运算符)
scores.sort()
print(scores) # [20, 23, 30, 34, 45, 56, 67, 78, 89]
scores.sort(reverse=True)
print(scores) # [89, 78, 67, 56, 45, 34, 30, 23, 20]
list.clear() -- 清空列表
list.copy() -- 复制列表 -- 复制列表中元素,产生一个新列表,和list[:]功能一样(浅拷贝)
new_scores = scores.copy()
print(new_scores) # [89, 78, 67, 56, 45, 34, 30, 23, 20]
练习:
打印列表中出现次数最多的元素
num = [1, 3, 5, 6, 3, 5, 7, 8, 1, 3, 2, 9, 1]
max1 = 0 #默认最大次数
for item in num:
count1 = num.count(item)
if count1 > max1:
max1 = count1
max_num = []
for item in num:
if max1 == num.count(item):
if item not in max_num:
max_num.append(item)
print(max_num)
练习2 去除列表中重复元素
num = [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1]
num1 = []
for item in num:
if item not in num1:
num1.append(item)
print(num1)
方法2:
num = [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1]
for item in num[:]:
if num.count(item) > 1: # 注意是在原列表中看元素出现的次数!
num.remove(item)
print(num)