Python编程:从入门到实践第四、五章列表(List)的定义及其基本操作笔记

列表

三、列表简介

  • 用负数下标访问列表
  • for循环
  • 插入
    • insert(位序,值) 插入函数
    • append() 末尾添加元素
  • 删除
    • del 删除元素,无返回值
    • pop 根据元素位序删除该元素(默认尾部),返回该元素值
    • remove 根据值删除最先出现的第一个元素,不存在则报错
  • 排序
    • sort() 永久排序(单调不减),reverse=True(逆向
    • sorted() 临时排序,不改变列表位序
  • reverse() 反转列表
  • len() 列表长度

四、操作列表

  • for 循环
  • 严格执行缩进
  • 冒号
  • 列表解析
  • range() 产生有序序列
  • 切片 开头:结尾:步进
  • 元组
    • 圆括号
    • 内容不可更改
    • 要更改元组可以重新定义
  • 代码格式
    • 易于阅读
    • 四个空格缩进
    • 每行不超过80字符
    • 空行
  • 3.1 列表定义

lbl=['name','age','weight','height','gender']
print(lbl)
#['name','age','weight','height','gender']

3.2 通过列表下表访问列表元素

lbl = ['name','age','weight','height','gender']
lbl_data = ['libing',22,74,178,'男']
print(lbl[0])
#name
print(lbl[1].upper())
#AGE
print(lbl[2]+":"+str(lbl_data[2]))
#weight:74

3.3 通过-1下标访问最后一个元素,-2访问倒数第二个元素,以此类推

# 当列表为空时,方位元素会出现错误
lbl = ['name','age','weight','height','gender']
print(lbl[-1])
print(lbl[-2])
print(lbl[-3])
#gender
#height
#weight

3.4 用for循环依次输出lbl元素

lbl = ['name','age','weight','height','gender']
for a in lbl:
    print(a)
# name
# age
# weight
# height
# gender

3.5 修改列表元素

lbl = ['name','age','weight','height','gender']
lbl[0] = 'degree'
print(lbl)
#['degree', 'age', 'weight', 'height', 'gender']

3.6 添加列表元素

lbl = ['name','age','weight','height','gender']
#append()表尾添加
lbl.append('degree')
print(lbl)#['name', 'age', 'weight', 'height', 'gender', 'degree']
#insert()在列表中任何位置插入元素
lbl.insert(0,'address')
print(lbl)#['address', 'name', 'age', 'weight', 'height', 'gender', 'degree']

3.7 从列表中删除元素 del pop remove

lbl = ['name','age','weight','height','gender']

#del 删除表中任何位置元素
del lbl[0]
print(lbl)
#['age', 'weight', 'height', 'gender']

#pop 获取列表任何位置元素的值并删除该元素
pop_value = lbl.pop(1)
print(pop_value)
print(lbl)
# gender
# ['age', 'weight', 'height']

# remove() 根据列表值删除第一个出现的元素,元素不存在则报错
lbl.remove('age')
print(lbl)# ['height', 'gender']

3.8.1 元素排序(永久排序 sort()

# sort() 更改内存中的值的排序
lbl = ['name','age','weight','height','gender']
lbl.sort()
print(lbl)
#['age', 'gender', 'height', 'name', 'weight']

3.8.2 元素排序(临时排序 sorted()

# sorted() 临时排序,单调不增,不改变列表元素实际位置,传入reverse=True参数倒着输出
lbl = ['name','age','weight','height','gender']
print(sorted(lbl))
print(sorted(lbl,reverse=True))
print(lbl)
# ['age', 'gender', 'height', 'name', 'weight']
# ['weight', 'name', 'height', 'gender', 'age']
# ['name', 'age', 'weight', 'height', 'gender']

3.9 反转列表元素 reverse()

lbl = ['name','age','weight','height','gender']
lbl.reverse()
print(lbl)
#['gender', 'height', 'weight', 'age', 'name']

3.10 确定列表长度 len()

lbl = ['name','age','weight','height','gender']
print(len(lbl))
# 5

4.1 遍历整个列表

lbl = ['name','age','weight','height','gender']
for a in lbl:
    print(a)
# name
# age
# weight
# height
# gender

4.2 for语句对列表的操作

lbl = ['name','age','weight','height','gender']
for item in lbl:
    if item=='age':
        print("my name is libinglin")
        print("age is 22")
#执行严格的缩进策略
# my name is libinglin
# age is 22

4.3 pizza

pizzas = ['apple','banana','pear','strewberry','mongo','pineapple','cheese']
for item1 in pizzas:
    print(item1)
for item2 in pizzas:
    print("i like "+item2+" pizza!")
print("i really like pizza!")
apple
# banana
# pear
# strewberry
# mongo
# pineapple
# cheese
# i like apple pizza!
# i like banana pizza!
# i like pear pizza!
# i like strewberry pizza!
# i like mongo pizza!
# i like pineapple pizza!
# i like cheese pizza!
# i really like pizza!

4.4 animal

animals = ['dog','cat','bear','sheep','lion','tiger']
print(len(animals))
for item in animals:
    print("A "+item+"would make a great pet!")
print("any of these animals would make a great pet!")

4.5 使用range()函数

# range(start,end,step,...) 生成一系列数字,从start到end,步进为step
for i in range(1,10,2):
    print(i)
# 1 3 5 7 9

4.6 使用range函数创建列表

numbers = list(range(1,10,2))
print(numbers)#[1, 3, 5, 7, 9]
squ = []
for i in range(1,10):
    #squ.append(i**2)
    squ.insert(i-1,i**3)#三次方
print(squ)
#[1, 4, 9, 16, 25, 36, 49, 64, 81]

4.7 数值列表处理函数 max() min() sum()

num = [1, 4, 9, 16, 25, 36, 49, 64, 81]
print(max(num))# 81
print(min(num))# 1
print(sum(num))# 285

4.8 列表解析

lists = [value**2 for value in range(1,11)]
print(lists)
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

4.9 一百万个数字列表

lists = [i for i in range(1,1000001)]
print(max(lists))
print(min(lists))
print(sum(lists))

4.10 3的倍数

lists = []
for i in range(3,31):
    if i % 3 ==0 :
        lists.append(i)
print(lists)#[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]

4.11 列表切片 开始:结束:步进

lists = [i for i in range(0,21)]
#开始:结束
print(lists[0:10])
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#开始:结束:步进
print(lists[0:20:2])
#[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
#开始:省略结束
print(lists[1:])
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
#省略开始:结束
print(lists[:5])
#[0, 1, 2, 3, 4]
#列表倒数元素,省略结尾
print(lists[-5:])
#[16, 17, 18, 19, 20]

4.12 复制列表

lists_1 = [value**2 for value in range(1,11)]

#浅拷贝
lists_2 = lists_1[:]
lists_2[0] = 999
print(lists_2)
print(lists_1)
#深拷贝
lists_3 = lists_1
lists_3[0] = 999
print(lists_3)
print(lists_1)
[999, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# [999, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# [999, 4, 9, 16, 25, 36, 49, 64, 81, 100]

4.13 元组定义

dim = (1,2,3,4,5)
print(dim)


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值