自学的资源太多,最终还是选择了Python编程:从入门到实践,我觉得不管选择哪本书,只要坚持到底,都是会有收获的,之前已经学习完成前三章的学习,抽空会把笔记上传上来,希望能够举一反三,独立思考。
4.3 创建数值列表
4.3.1 使用函数range()
Python函数range()让你能够轻松地生成一系列的数字。
eg:
for value in range(1,5):
print(value)
op:
1
2
3
4
注意:range(1,5)指定从1开始,在到达指定的倒数第2个停止。
4.3.2 使用range()创建数字列表(可这样使用:list(range())
1. 创建数字列表,可使用函数list()将range()的结果直接转换为列表。如果将range()作为list()的参数,输出将为一个数字列表。
eg:
numbers = list(range(1,6))
print(numbers)
op:
[1, 2, 3, 4, 5]
2. 可指定步长,即range(number1,number2,stepsize)
eg:
numbers = list(range(1,11,2))
print(numbers)
op:
[1, 3, 5, 7, 9]
3. 使用函数range几乎能够创建任何需要的数字集,在Python中,两个星号(**)表示乘方运算,
eg:
squares = []
for value in range(1,11):
square = value ** 2
squares.append(square)
print(squares)
op:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
解释: 首先创建了一个空的列表squares;然后使用函数range()让Python遍历1-10的值。在循环中创建了一个变量square并对其赋值当前值的平方,再然后将当前值插入列表squares末尾。最后,循环结束后打印列表squares。
之前eg可对简化代码,如下:
eg:
squares = []
for value in range(1,11):
squares.append(value ** 2)
print(squares)
op:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
注意:创建更复杂的列表时,可使用上述两种方法中的任何一种。
有时候,使用临时变量会让代码 更易读;
而在其他情况下,这样做只会让代码无谓地变长。
首先应该考虑的是,编写清晰易懂 且能完成所需功能的代码;
等到审核代码时,再考虑采用更高效的方法。
4.3.3 对数字列表执行简单的统计计算(min()、max()、sum())
eg:
a = min(range(1,11))
b = max(range(1,11))
c = sum(range(1,11))
print(a,b,c)
op:
1 10 55
4.3.4 列表解析
4.3.2 介绍生成列表squares包含三四行代码,列表解析只需要一行就可以生成。
列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素。
eg:
squares = [value ** 2 for value in range(1,11)]
print(squares)
op:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
注意:1. 使用此语法,首先指定列表名;
2. 指定左边方括号([),定义表达式,生成要存储在列表中的值,此例表达式为value ** 2
3. 写1个for循环,作用:给表达式提供值,最后加上右方括号。
4.4 使用列表的一部分
4.4.1 切片(指定第一个和最后一个元素的索引)
切片:处理列表的部分元素
从头开始:
eg:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3]) #此为1个切片,即打印索引从0开始,终止与3前面的元素(也就是2),最终输出结果有3个
print(players[:3] ) #第一个索引未指定时,默认从头开始
op:
['charles', 'martina', 'michael']
注意:1. 索引从0开始;
2. 索引终止位置的前一位元素打印出来
从中间开始到中间结束:
eg:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])
op:
['martina', 'michael', 'florence']
从中间开始到列表末尾结束:
eg:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[2:])
op:
['michael', 'florence', 'eli']
只要后3位:
eg:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3,]
op:
['michael', 'florence', 'eli']
4.4.2 遍历切片
如果要遍历列表的部分元素,可在for循环中使用切片。
eg:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players in our team:")
for player in players[:3]:
print(player.title())
op:
Here are the first three players in our team:
Charles
Martina
Michael
4.4.3 复制列表
要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:])。
这让Python创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个列表。
eg:
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods =my_foods[:]
#若写成friend_foods =my_foods,这里将my_foods赋给friend_foods,而不是将my_foods的副本存储到friend_foods
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
op:
My favorite foods are:
['pizza', 'falafel', 'carrot cake']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake']
小结:1. 效率高,望坚持。在西北工业大学自习效率很高,学习氛围还是很重要的;
2. 减少犯常见错误的次数。比如for循环经常忘记输入冒号:,会把单词打错等
2. 减少犯常见错误的次数。比如for循环经常忘记输入冒号:,会把单词打错等
本文介绍了Python中使用range()函数创建数值列表的方法,包括指定步长、执行统计计算及列表解析等高级技巧。此外,还详细讲解了如何通过切片来处理列表的部分元素,以及复制列表的正确方式。
1311

被折叠的 条评论
为什么被折叠?



