《python编程:从入门到实践》极简笔记:操作列表

遍历整个列表

使用for循环打印魔术师名单中的所有名字

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

创建数值列表

存储一组数字

使用函数range()

for value in range(1,5):
    print(value)
1
2
3
4

range()输出不包含第二个值

使用range()创建数字列表

用函数list()讲=将range()的结果直接转换为列表。

number=list(range(1,6))
print(number)
[1, 2, 3, 4, 5]

使用函数range时可指定步长。例如,打印1~10内的偶数

even_number=list(range(2,11,2))
print(even_number)
[2, 4, 6, 8, 10]

range从2开始数,然后不断加2,直至达到或超过终值(11)
两个星号(**)表示乘方运算。下面代码演示了如何将前10个整数的平方加入到一个列表中:

squares=[]
for value in range(1,11):
    square=value**2
    squares.append(square)
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

简洁版

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

对数字列表执行简单的统计计算

数字列表的最大值最小值和总和,分别为max(),min(),sum()

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

列表解析

列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素

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

使用列表的一部分

切片

要创建切片,可指定要使用的第一个元素和最后一个元素的索引,只包含第一个索引不包含第二个

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

若没有指定第一个索引。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']

负数索引返回离列表末尾相应距离的元素。例如如果要输出名单上的最后三名队员,可使用切片player[-3:]:

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

遍历切片

for循环遍历列表的部分元素

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

复制列表

你经常需要根据既有列表创建全新的列表。可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:]).

my_food=['pizza','falafel','carrot cake']
friend_food=my_food[:]
my_food.append('ice cream')
friend_food.append('reganmian')
print('my favotite foos are :')
print(my_food)
print("my friend's favotite food are :")
print(friend_food)
my favotite foos are :
['pizza', 'falafel', 'carrot cake', 'ice cream']
my friend's favotite food are :
['pizza', 'falafel', 'carrot cake', 'reganmian']

直接print(my_food.append(‘ice cream’))显示none
若不使用切片的方法,直接令两个列表相等,则输出结果也相等

my_food=['pizza','falafel','carrot cake']
friend_food=my_food
my_food.append('ice cream')
friend_food.append('reganmian')
print('my favotite foos are :')
print(my_food)
print("my friend's favotite food are :")
print(friend_food)
my favotite foos are :
['pizza', 'falafel', 'carrot cake', 'ice cream', 'reganmian']
my friend's favotite food are :
['pizza', 'falafel', 'carrot cake', 'ice cream', 'reganmian']

元组

列表是可以修改的,元组不可修改。不可变的列表称为元组。

定义元组

用圆括号标识

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

索引仍用中括号
若尝试修改则会报错

dimensions=(200,5)
dimensions[0]=250
print(dimensions[0])
Traceback (most recent call last):
  File "d:/PythonPractice/practice/test.py", line 2, in <module>
    dimensions[0]=250
TypeError: 'tuple' object does not support item assignment

遍历元组

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

修改元组变量

给存储元组的变量赋值是可以的

dimensions=(200,5)
for dimension in dimensions:
    print(dimension)
dimensions=(8293,293849,1930)
print(dimensions)
200
5
(8293, 293849, 1930)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值