1. 列表相关运算
1.1 数学运算: +, *
列表1+列表2 - 将两个列表中的元素合并产生一个新的列表(原列表不会发生改变)
列表*N - 列表中的元素重复N次产生新的列表
list1 = [1, 2, 3]
list2 = ['name', 'age']
new_list = list1 + list2
print(new_list, list1, list2)
print(list1*3)
list3 = [1]*100
print(list3)
1.2 比较运算:
==, !=
list4 = [1, 2]
print([1, 2] == [2, 1]) # False
print([1, 2] != [2, 1]) # True
print([1, 2] == list4) # True
>, <, >=, <= 只支持相同位置上元素类型相同的两个列表 (了解)
print([1, '2', 3] > [100, 'z'])
# print([1, 2, 3] > ['a', 'b']) #TypeError: '>' not supported between instances of 'int' and 'str'
2.内置函数
- len(列表)、sum(列表) 要求列表中的元素必须是数字、
- max(列表)、min(列表)
- list(数据) - 将指定数据转换成列表, 所有的序列都可以转换成列表,比如: 字符串,列表、字典、集合、元祖、range、迭代器、生成器等。
转换的时候是将序列中的元素作为列表元素
# print(sum(['1', 'abc', 'hj']))
# print(max([1, 'a']))
print(list('abcd'))
print(list(range(5)))
print(list(str(123)))
nums = ['10', '23', '4'] # 10234
print(''.join(nums))
3.相关方法
3.1 列表.count(元素) - 获取指定元素在列表中出现的次数, 结果是整数
print([1, 2, 3, 1, 4].count(1))
3.2 列表.extend(序列) - 将序列中的元素添加到列表中, 结果None
list1 = []
list1.extend('abc')
print(list1)
list1.extend(range(3))
print(list1)
list1.extend(['张飞', '关羽', '赵云'])
print(list1)
list1.extend(['吕布'])
print(list1)
3.3 列表.index(元素) - 获取指定元素在列表中的下标,如果这个元素有多个只获取第一个,如果元素不存在会报错
list2 = [1, 0, 1, 2, '张飞', '关羽', '赵云', '吕布']
print(list2.index('张飞'))
# print(list2.index('诸葛亮')) # ValueError: '诸葛亮' is not in list
3.4 列表.reverse() - 将列表中的元素倒序, 不会产生新列表
list2.reverse()
print(list2)
补充: 内置函数: reversed(序列) - 将序列中的元素倒序产生一个新的迭代器, 原序列不会修改
list1 = [1, 2, 3]
result = reversed('abc')
# for item in result:
# print(item)
print(list(result))
print(list1)
3.5 列表.sort() - 对列表中的元素从小到大排序(列表中的元素类型一致并且支持比较运算), 不会产生新的列表
列表.sort(reverse=True) - 从大到小排序
scores = [10, 100, 89, 20, 67, 34, 9]
# scores.sort()
# print(scores) # python cookbook/ 流畅的python
scores.sort(reverse=True)
print(scores)
3.6 列表.copy() - 产生一个新的列表,列表中的元素和原列表一样,相当于: 列表[:]
list1 = [1, 2, 3]
list2 = list1
list3 = list1.copy()
print(list2, list3)
list1.append(10)
print(list2, list3)
3.7 id(变量) - 查看变量中实质存储的地址
python所有变量实质都是直接保存的数据在内存中的地址
print(id(list1), id(list2), id(list3))
4. 列表中删除数据
删除列表中所有小于60的数字
4.1 问题1: 通过元素删除的时候可能出现删不干净的问题**
scores = [10, 50, 90, 89, 45, 70]
for score in scores:
if score < 60:
scores.remove(score)
print(scores, score) # [50, 90, 89, 20, 70]
"""
scores = [10, 50, 90, 89, 45, 70]
score = 10 if 10 < 60 scores.remove(10) scores = [50, 90, 89, 45, 70]
score = 90 if 90 < 60
score = 89 if 89 < 60
score = 45 if 45 < 60 scores.remove(45) scores = [50, 90, 89, 70]
"""
解决问题1:
scores = [10, 50, 90, 89, 45, 70]
scores2 = scores[:]
for score in scores2:
if score < 60:
scores.remove(score)
del scores2 # score2只是提供遍历用的,用完后没有其他用处,可以直接删除
print(scores, score)
上面的简写
scores = [10, 50, 90, 89, 45, 70]
for score in scores[:]:
if score < 60:
scores.remove(score)
print(scores, score)
"""
程序过程:
scores = [10, 50, 90, 89, 45, 70]
scores2 = [10, 50, 90, 89, 45, 70]
score = 10 if 10<60 scores.remove(10) scores = [50, 90, 89, 45, 70]
score = 50 if 50<60 scores.remove(50) scores = [90, 89, 45, 70]
score = 90 if 90<60
score = 89 if 89<60
score = 45 if 45<60 scores.remove(45) scores = [90, 89,70]
score = 70 if 70<60
"""
4.2 问题2:通过下标删除满足要求的元素的时候,出现下标越界的错误
scores = [10, 50, 90, 89, 45, 70]
for index in range(len(scores)):
if scores[index] < 60:
del scores[index]
print(scores)
"""
scores = [10, 50, 90, 89, 45, 70]
for index in range(6)
index = range(6) index = (0,1,2,3,4,5)
index = 0 if scores[0]<60 if 10<60 del scores[0] scores = [50, 90, 89, 45, 70]
index = 1 if scores[1]<60 if 90<60
index = 2 if scores[2]<60 if 89<60
index = 3 if scores[3]<60 if 45<60 del scores[3] scores = [50, 90, 89, 70]
index = 4 if scores[4]<60 (out of range)
"""
解决问题2:
scores = [10, 50, 90, 89, 45, 70]
index = 0
while index < len(scores):
if scores[index] < 60:
del scores[index]
continue
index += 1
print(scores)
"""
scores = [10, 50, 90, 89, 45, 70]
index = 0
while 0 < 6 if 10<60 del scores[0] scores = [50, 90, 89, 45, 70]
while 0 < 5 if 50<60 del scores[0] scores = [90, 89, 45, 70]
while 0 < 4 if 90<60 index += 1 index = 1
while 1 < 4 if 89<60 index += 1 index = 2
while 2 < 4 if 45<60 del scores[2] scores = [90, 89,70]
while 2 < 3 if 70<60 index += 1 index = 3
while 3 < 3
print(scores) - [90, 89,70]
"""
网友评论