Python基础——#3操作列表

#3操作列表

遍历列表 for

Python的for循环语法结构 for a in b: #a是列表b中的一个元素。(不要忘记冒号)

for循环执行过程:先取 b中第一个值,存储与a中,然后执行for循环里的代码;由于b中还有其他值,则继续执行for,直到b中的值均遍历一遍为止。

countries=['china','korean','british','american','australian']
for country in countries:
	print(country)

运行结果:

china
korean
british
american
australian

同样可以搭配第一章内容使用

print("\n\n\n")
countries=['china','korean','british','american','australian']
for country in countries:
	print(country.title()+" is a country!")

运行结果:

China is a country!
Korean is a country!
British is a country!
American is a country!
Australian is a country!

for循环中可以包含多条语句

Python中不用{ }来限制for循环的循环体,而是通过对齐方式的不同,来确定循环体内和外

For instance

print("\n\n\n")
countries=['china','korean','british','american','australian']
for country in countries:
	print(country.title()+" is a country!")
	print(country.upper()+" is a name of a country!")
print("That's all thank you!"+country)

运行结果:

China is a country!
CHINA is a name of a country!
Korean is a country!
KOREAN is a name of a country!
British is a country!
BRITISH is a name of a country!
American is a country!
AMERICAN is a name of a country!
Australian is a country!
AUSTRALIAN is a name of a country!
That's all thank you!australian

最后一句未缩进所以只执行一次,而最后的australian你知道为什么吗?

因为在for循环执行的时候,country的最后一次赋值是australian,所以在下一次输出时自然输出的时australian。

同时我们还发现,缩进在Python程序中非常重要,缩进错误或许不会导致语法错误,但将会导致程序逻辑错误。

至此,我们学习的语法规则,只有for循环的循环体语句需要缩进,之后还有哪些语句需要缩进呢,让我们拭目以待!

创建数字列表

range()

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

Result:

1
2
3
4

没有出现数字 5 ,故猜测range()的工作原理是,1<=x<5

`list()

使用list()可以将range()的值直接转化为列表

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

Result:

[1, 2, 3, 4]

range()函数还可以指定 步长

print("\n\n\n")
for value in range(1,7,2):
	print(value)

Result:

1
3
5

扩展想象力,range可以创建任何需要的数字集

1-10的平方

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

Result:

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

以上代码还可以进行简化

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

统计数字列表

squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
min_squares=min(squares)
max_squares=max(squares)
sum_squares=sum(squares)
print(min_squares)
print(max_squares)
print(sum_squares)

Result:

1
100
385

列表解析

化简以上代码

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

Result:

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

使用列表的一部分

切片

列表名[起始索引,终止索引+1]

squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print(squares[0:3])

Result:

[1, 4, 9]

若冒号前为指定起始索引,则默认从列表头开始;

若冒号后未指定终止索引,则默认到表尾。

squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print(squares[:4])
print(squares[2:])

Result:

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

-2则从倒数第二位开始

print(squares[-2:])

Result:

[81, 100]

每个切片就相当于一个子列表,子列表也可以通过for循环遍历

squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
for square in squares[3:8]:
	print(square)
print("That's end of the FOR")

Result:

16
25
36
49
64
That's end of the FOR

复制列表

将切片的起始索引和末尾索引都为空[:]

squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
s_squares=squares[:]
print(s_squares)		

Result:

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

元组(不可变的列表)

元组与列表的区别是用圆括号来表示

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

Result:

200
10

试图修改元组的值就会报错

遍历元组

for dimension in dimensions:
	print(dimension)

Result:

200
50
40
10

修改元组变量

元组宗旨,要么不变,要么全变

不可修改元组的元素,但可以给存储元组的变量重新赋值。

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

print("\n")
dimensions=(40,2,86,3)
for dimension in dimensions:
	print(dimension)

Result:

200
50
40
10

40
2
86
3

上一篇:Echarts制作一张全球疫情图


下一篇:Go 语言Map(集合)