Python-4.1-操作列表

操作列表

遍历整个列表

  • 使用for循环遍历列表的所有元素,对每个元素执行相同的操作
    • 在for循环中执行操作
    • 在for循环结束后执行操作
#for循环遍历列表
animals = ['dog','monkey','tiger']
for animal in animals:
    print(animal.title() + " is cute animal!")

print('\n')

#for循环结束后执行操作
animals = ['dog','monkey','tiger']
for animal in animals:
    print(animal.title() +" is cute animal!")
print("I like animals")

Dog is cute animal!
Monkey is cute animal!
Tiger is cute animal!

Dog is cute animal!
Monkey is cute animal!
Tiger is cute animal!
I like animals

避免缩进错误

  • 忘记缩进
    • 对于位于for语句后面且属于循环组成部分的代码行,一定要缩进
  • 忘记缩进额外的代码行
  • 不必要的缩进
  • 循环后不必要的缩进
  • 遗漏了冒号

创建数值列表

  • 使用函数range()
    • 函数range()让Python从你指定的第一个数开始数,并在到达你指定的第二个值后停止,因此输入不包含第二个值
#使用函数range()来打印一系列的数字
for value in range(1,5):
    print(value)

1
2
3
4

  • 使用range()创建数字列表
    • 将range()作为list()参数,输出将为一个数字列表
      • 创建数字列表
      • 指定步长
        • 打印出1-10之间的偶数
      • 在Python中,两个星号(**)表示乘法运算
        • 打印出前10个整数的平方
#创建数字列表
number = list(range(1,5))
print(number)

print('\n')

#指定步长
a = list(range(2,11,2))
print(a)

print('\n')

#乘法运算
a = []
for b in range(1,11):
    c = b**2
    a.append(c)
print(a)

[1, 2, 3, 4]

[2, 4, 6, 8, 10]

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

  • 对数字列表执行简单的统计计算
    • 打印出列表的最大值
    • 打印出列表的最小值
    • 打印出列表的总和
#最大值
digits = [8,7,2,9,4,15,1]
max(digits)

15

#最小值
digits = [8,7,2,9,4,15,1]
min(digits)

1

#总和
digits = [1,2,3,4,5,6,7,8,9]
sum(digits)

45

  • 列表解析
    • 列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素
a = []
for b in range(1,11):
    c = b**2
    a.append(c)
print(a)

a = []
for b in range(2,11,2):
    a.append(b)
print(a)

#列表解析
a = [b**1 for b in range(3,11,2)]
print(a)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

[2, 4, 6, 8, 10]

[3, 5, 7, 9]

使用列表的一部分

  • 切片
    • 处理列表的部分元素
#切片
players = ['charles','martina','michael','florence','eli']
print(players[0:2])
print(players[1:])
print(players[:4])
print(players[-1:])
print(players[:])
print(players[0:1] + players[2:3])

[‘charles’, ‘martina’]
[‘martina’, ‘michael’, ‘florence’, ‘eli’]
[‘charles’, ‘martina’, ‘michael’, ‘florence’]
[‘eli’]
[‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘eli’]
[‘charles’, ‘michael’]

  • 遍历切片
    • 可使用for循环遍历列表的部分元素
players = ['charles','martina','michael','florence','eli']
for player in players[:2]:
    print("The best player is " + player.title() + "\n")
print("Everybody is best player")

The best player is Charles

The best player is Martina

Everybody is best player

  • 复制列表
    • 可创建一个包含整个列表的切片
      • 同时省略起始和终止索引([:])。创建一个始于第一个元素,终止于最后一个元素的切片。
#复制列表
one_lesson = ['mach','chinses','music']
two_lesson = one_lesson[:]
#在one_lesson列表中添加history
one_lesson.append("history")
#在two_lesson列表中田间english
two_lesson.append("english")
print(one_lesson)
print(two_lesson)

[‘mach’, ‘chinses’, ‘music’, ‘history’]

[‘mach’, ‘chinses’, ‘music’, ‘english’]

元组

  • 定义元组
    • Python将不能修改的值称为不可变的,而不可变的列表被称为元组
  • 遍历元组中的所有值
#定义元组
animals = ('dog','tiger','monkey')
print(animals[0])
print(animals[1])
print(animals[2])

print('\n')

#遍历元组的元素
animals = ('dog','tiger','monkey')
for animal in animals:
    print(animal)

#修改元组的元素时
animals = ('dog','tiger','monkey')
animals[0]='asd

dog
tiger
monkey

dog
tiger
monkey

TypeError                                 Traceback (most recent call last)
<ipython-input-20-ac368659b628> in <module>
     16 #修改元组的元素时
     17 animals = ('dog','tiger','monkey')
---> 18 animals[0]='asd'

TypeError: 'tuple' object does not support item assignment

  • 修改元组中的变量
    • 虽然不能修改元组中的元素,但可以给存储元组的变量赋值
#修改元组中变量
dimensions = (50,200)
print("Orginal dimension is")
for dimension in dimensions:
    print(dimension)
    
print('\n')

dimensions = (100,400)
print("Moddfied dimension is")
for dimension in dimensions:
    print(dimension)

Orginal dimension is
50
200

Moddfied dimension is
100
400

设置代码格式

  • 格式设置指南
    • Python改进提案(Python Enhancement Proposal,PEP)
  • 缩进
    • PEP 8建议每级缩进都使用四个空白
  • 行长
    • PEP 8建议注释的行长不超过72字符
  • 空行
    • 要将程序的不同部分,可使用空行
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值