Python 操作列表


(1) for循环遍历列表

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician)

输出

alice
david
carolina

Python 根据缩进判断代码行与前一代码行的关系。上例中,print这一行只有缩进了才是for循环的一部分。不必要的缩进不能缩进,必要的缩进必须缩进。

for value in range(1, 5):
    print(value)

输出

1
2
3
4

函数range()让Python从你指定的第一个值开始数,并在到达你指定的第二个值后停止。输出不包含第二个值(这里为5)。

(2)使用 range() 创建数字列表

numbers = list(range(1, 6))
print(numbers)

输出

 [1, 2, 3, 4, 5]

(3)range() 指定步长

even_numbers = list(range(5, 13, 2))
print(even_numbers)

输出

[5, 7, 9, 11]

从5开始,不断地加2,直到【达到或超过】终值13。

(4)前10个整数的平方加入到一个列表中

squares = []
for value in range(1, 11):
    square = value**2  # **表示乘方运算,**2即value的平方
    squares.append(square)
print(squares)

输出

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

(5)简单的统计计算

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits))
print(max(digits))
print(sum(digits))

输出

0
9
45

(6)列表解析(一行代码就可以生成上述的 squares 列表)

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

输出

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

for 循环为 for value in range(1,11),它将值1~10提供给表达式 value**2

(7)学习了如何访问单个列表元素,也学习了如何处理列表的所有元素。Python还可以处理列表的部分元素——Python称之为切片。

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3]) # 0:3是起始索引:结束索引

输出

['charles', 'martina', 'michael']

又如

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])

输出

['martina', 'michael', 'florence']

(8)没有指定第一个索引, Python将自动从列表开头开始

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])

输出

['charles', 'martina', 'michael', 'florence']

同理

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[2:])

输出

['michael', 'florence', 'eli']

(9) 换个负数索引

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])

输出

['michael', 'florence', 'eli']

(10)遍历切片

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players on my team:")
for player in players[:3]:
    print(player.title())

输出

Here are the first three players on my team:
Charles
Martina
Michael

(11)复制列表

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]

print("My favorite foods are:")
print(my_foods)

print("\nMy friend's favorite foods are:")
print(friend_foods)

输出

My favorite foods are:
['pizza', 'falafel', 'carrot cake']

My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake']

(12)追加food

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)

输出

My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli']

My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']

(13)元组(不可变的列表)

dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])

输出

200
50

(14)遍历元组

dimensions = (200, 50)
for dimension in dimensions:
    print(dimension)

输出

200
50

(15)修改元组变量(虽然不能修改元组的元素,但可以给存储元组的变量赋值。)

dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
    print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
    print(dimension)

输出

Original dimensions:
200
50

Modified dimensions:
400
100
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值