python基础---列表

列表

1.列表的作用

需要同时处理多个数据的时候就可以使用列表

# 场景:定义变量保存一个班所有学生的分数
# 1)不使用列表
score1 = 90
score2 = 78
score3 = 91
score4 = 62
score5 = 77
score6 = 80
score7 = 93
score8 = 99
score9 = 58
score10 = 64

print('平均分:', (score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8 + score9 + score10)/10)

max_score = score1
if score2 > max_score:
    max_score = score2
if score3 > max_score:
    max_score = score3
if score4 > max_score:
    max_score = score4
if score5 > max_score:
    max_score = score5
if score6 > max_score:
    max_score = score6
if score7 > max_score:
    max_score = score7
if score8 > max_score:
    max_score = score8
if score9 > max_score:
    max_score = score9
if score10 > max_score:
    max_score = score10
print('最高分:', max_score)

print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~华~丽~丽~的~分~割~线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')

# 2)使用列表
scores = [90, 78, 91, 62, 77, 80, 93, 99, 58, 64]
print('平均分:', sum(scores)/len(scores))
print('最高分:', max(scores))

2. 什么是列表(list)

  • 列表是python自带的容器型数据类型,将[]作为容器的标志,里面多个元素(数据)用逗号隔开:[数据1, 数据2, 数据3, …]
  • 列表是可变的(可变指的是元素的个数和元素的值可变)-支持增删改;列表是有序的(顺序影响结果,并且支持索引操作)
  • 列表对元素没有要求,任何数据都可以作为列表的元素
# 列表是有序的
print([10, 20, 30] == [20, 10, 30])
# 集合是无序的
print({10, 20, 30} == {20, 10, 30})
# 空列表
list1 = []

一.列表的查操作

1. 查单个 - 获取列表中的一个元素

语法:

列表[索引]

说明:
列表 ---- 可以是具体的一个列表数据,也可以是保存列表的变量
[] -------- 固定写法
索引 — 又叫下标,是列表中元素的位置信息 列表一旦确定,列表中元素的索引值就确定: 元素从前往后对应的索引值从0开始不断加1; 元素从后往前对应的索引值从-1开始不断减1

scores = [90, 87, 99, 61, 36, 45]
print(scores[1])
print(scores[-1], scores[5])
# 注意:索引值不能越界
# print(scores[6])

2. 切片 - 获取列表中的部分元素

语法:

列表[开始下标:结束下标] - 获取开始下标和结束下标确定的范围内的所有元素(开始下标对应的值能取到,结束下标对应的元素取不到)

scores = [90, 87, 99, 61, 36, 45]
print(scores[0:-3])
print(scores[1:-1])
# 注意:如果要获取到最后一个元素,可以省略结束下标
print(scores[2:], scores[2:6])

3. 遍历 - 一个一个地获取列表中所有的元素

方法1:
for 变量 in 列表:
循环体(变量依次获取到的是列表中的每个元素)

方法2:- 依次获取每个元素的索引值,来获取每个元素
for x in range(列表长度):
循环体(变量依次取到的是每个元素索引值, 可以通过列表[索引]来获取对应的元素)

scores = [90, 87, 99, 61, 36, 45]
for x in scores:
    print(x)
    
scores = [90, 87, 99, 61, 36, 45]
# len(列表)  -  获取列表中元素的个数
for x in range(len(scores)):
    print(x, scores[x])
    
# 5 4 3 2 1 0
for x in range(len(scores)-1, -1, -1):
    print(x, scores[x])
    
# -1 -2 -3 ... -6
for x in range(-1, -len(scores)-1, -1):
    print(x, scores[x])

二.列表的增删改

1. 增 - 添加元素

1)列表.append(元素) - 在指定列表的最后添加指定的元素

# 案例1:在nums中的每个偶数的后面添加一个元素0
nums = [89, 67, 90, 44, 52, 51, 37]
new_nums = []
for x in nums:
    new_nums.append(x)
    if x % 2 == 0:
        new_nums.append(0)
print(new_nums)

# 练习1:获取nums中所有的奇数
nums = [82, 67, 90, 44, 52, 51, 37, 90]
new_nums = []
for x in nums:
    if x % 2 != 0:
        new_nums.append(x)
print(new_nums)

# 练习2:将nums中所有的偶数都除以2
nums = [82, 67, 90, 44, 52, 51, 37, 90]
new_nums = []
for x in nums:
    if x % 2 != 0:
        new_nums.append(x)
    else:
        new_nums.append(x // 2)
print(new_nums)

# 练习3:将scores中所有的0分都替换成'补考'
scores = [89, 67, 90, 0, 44, 0, 52, 0, 51, 37]
new_nums = []
for x in scores:
    if x == 0:
        new_nums.append('补考')
    else:
        new_nums.append(x)
print(new_nums)

2)列表.insert(索引, 元素) - 将列表中指定索引对应的元素前插入指定元素

# 练习:在nums中的第一个负数的前面插入100
nums = [190, 89, 6, -28, 89, -298, 10]
for x in range(len(nums)):
    if nums[x] < 0:
        nums.insert(x, 100)
        break
print(nums)

2. 删 - 删除元素

1)del 列表[索引] - 删除列表中指定索引对应的元素

movies = ['魁拔', '肖生克的救赎', '沉默的羔羊', '绿皮书', 'V字仇杀队', '间谍过家家', '霸王别姬', '战狼']
print(movies)

del movies[-2]
print(movies)

2)列表.remove(元素) - 删除列表中指定元素

movies = ['魁拔', '肖生克的救赎', '沉默的羔羊', '绿皮书', 'V字仇杀队', '间谍过家家', '霸王别姬', '战狼']
print(movies)

movies.remove('肖生克的救赎')
print(movies)

3)列表.pop() - 取走列表中最后一个元素并且返回
列表.pop(索引) - 取走列表中指定索引对应的元素并且返回

movies = ['魁拔', '肖生克的救赎', '沉默的羔羊', '绿皮书', 'V字仇杀队', '间谍过家家', '霸王别姬', '战狼']
print(movies)

result = movies.pop(2)
print(movies)
print(result)

3. 改 - 修改元素的值

列表[索引] = 数据 - 将列表中指定索引对应的元素修改成指定的数据

movies = ['魁拔', '肖生克的救赎', '沉默的羔羊', '绿皮书', 'V字仇杀队', '间谍过家家', '霸王别姬', '战狼']
print(movies)

movies[2] = '蝴蝶效应'
print(movies)

三.列表相关操作

1. 数学运算:+、*

1)列表1 + 列表2 - 创建一个新的列表,列表中元素就是原来两个列表中的所有元素(列表的合并)

nums1 = [10, 20, 30]
nums2 = [100, 200]
result = nums1 + nums2
print(result)       # [10, 20, 30, 100, 200]

2)列表 * N - 将列表中的元素重复N次产生一个新的列表

result = nums1 * 3
print(result)

2. 比较运算符

  1. 比较是否相等: ==、!=

两个一模一样的列表才相等

print(nums1 == [10, 20, 30])        # True
print(nums1 == [20, 10, 30])        # False
  1. 比较大小: 两个列表可以比较大小,比较的是第一对不相等的元素的大小
print([10, 100, 200, 300] > [20, 1])            # False
print([10, 100, 200, 300] > [10, 200])

3. in和not in

数据 in 列表 - 判断列表中是否存在指定数据对应的元素

数据 not in 列表 - 判断列表中是否不存在指定数据对应的元素

nums1 = [10, 20, 30, 100]
print(100 in nums1)
print([10, 20] in nums1)

nums1 = [[10, 20], 30, 100]
print(10 in nums1)
print([10, 20] in nums1)

4. 列表相关函数 - sum、max、min、sorted、len、list

sum(列表)求列表中所有元素的和
max(列表)求列表中元素的最大值
min(列表)求列表中元素的最小值
sorted(列表) sorted(列表, reverse=True)将列表中的元素升序排序,产生一个新的列表 将列表中的元素降序排序,产生一个新的列表
len(列表)获取列表中元素的个数
list(数据)将指定数据转换成列表(所有的容器型数据类型的数据都可以转换成列表)
scores = [90, 87, 99, 61, 36, 45]
print(sum(scores))
print(max(scores))
print(min(scores))

result = sorted(scores)
print(result)       # [36, 45, 61, 87, 90, 99]

result = sorted(scores, reverse=True)
print(result)       # [99, 90, 87, 61, 45, 36]

print(len(scores))

print(list('abc123'))       # ['a', 'b', 'c', '1', '2', '3']
print(list(range(5)))

5. 列表相关方法

  1. 列表.clear() - 清空列表
movies = ['魁拔', '肖生克的救赎', '沉默的羔羊', '绿皮书', 'V字仇杀队', '间谍过家家', '霸王别姬', '战狼']
movies.clear()
print(movies)
  1. 列表.count(元素) - 统计列表中指定元素的个数
nums = [10, 23, 90, 10, 89, 90, 10, 80, 91]
print(nums.count(10))
print(nums.count(90))
print(nums.count(100))
  1. 列表.index(元素) - 获取列表中第一个指定元素对应的索引(0开始的索引值)
print(nums.index(10))
print(nums.index(90))
  • 27
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值