【Python基础知识】(三)遍历列表、切片和元组

遍历列表(for循环)

1、使用for循环打印列表中的元素

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

 输出为:

alice
david
carolina

  (1)临时变量magician可替换为任何其他名称,如person,a,M等等。

  (2)关于执行顺序:在实际运行中,Python会反复执行for语句和print语句,直到遍历完列表中所有元素

  (3)Python中print语句的句尾结束符为 (执行完一次输出后自动换行)。

2、一次循环中打印多条语句

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician)
    print(magician.title() + ", that was a great trick!
")
print("Thank you, everyone. That was a great magic show!")

 输出为:

alice
Alice, that was a great trick!
david David, that was a great trick!
carolina Carolina, that was a great trick!
Thank you, everyone. That was a great magic show!

  (4)Python中,在for循环后,可包含任意行代码。和C语言中用大括号来表示循环体内包含的语句的方式不同,Python用缩进的方式来表示这些语句。在循环语句冒号后的每个缩进的代码行都是循环的一部分,且针对列表中的每个值都执行一次,而在for循环后面,没有缩进的代码只执行一次,不会重复执行。

  (5)for语句后面紧跟的语句一定至少有一句要缩进,否则会出现缩进错误(IndentationError)。当缩进了无需缩进的代码行时也会出现此类错误。

数字列表

1、使用range函数创建数字列表

  (1)range( 下限数字m, 上限数字n ):生成包含从m到n之间的所有自然数的数字列表(包括m但不包括n)。

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

 输出为:

2
3
4

  所以要打印数字1~5,就要调用range( 1, 6 )

  (2)list( range( 下限数字m, 上限数字n ) ):以数字列表的形式输出range()。

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

 输出为:

[1, 2, 3, 4, 5]

 2、逐个元素创建数字列表

  利用循环语句逐个向列表中添加元素。

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]

   

3、列表解析法创建数字列表

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

 输出为

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

4、处理数字列表的函数

  (1)min( 数字列表名称 ):求数字列表中元素的最小值

  (1)max( 数字列表名称 ):求数字列表中元素的最大值

  (1)sum( 数字列表名称 ):求数字列表中所有元素的总和

digits = [1, 2, 3, 0]

min(digits) = 0
max(digits) = 3
sum(digits) = 6

切片

1、切片的概念

  切片( 列表名称[ 起始索引 : 终止索引 ] ):可用来处理列表中的部分元素。

  [ m : n ]:提取列表中索引从m到(n-1)的所有元素。

  [  : n ]没有指定起始索引,切片将会从列表表头开始提取。

  [ m :  ]没有指定终止索引,切片将会终止于列表表尾

  [ -m :  ]:当起始索引为负数没有指定终止索引时,切片将提取列表中最后m个元素。

  [ m : n : 2 ]:以步长为2,提取列表中索引从m到n的元素。

  [ m : n : -1 ]:以步长为1反向提取列表中的元素。

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

print(players[ : 4])    
print(players[1 : ])    

print(players[-3 : ])    

 输出为:

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

  

2、遍历切片

  形式与数字列表类似。

players = ['charles', 'martina', 'michael', 'florence', 'eli']
for player in players[ : 3]:
    print(player.title())

 输出为:

Charles
Martina
Michael

 3、使用切片复制列表

the_players = ['charles', 'martina', 'michael', 'florence', 'eli']
our_players = the_players[ : ]
print(the_players)
print(our_players)  '''复制列表'''

the_players.append('eric')
our_players.append('jack')
print(the_players)
print(our_players)  '''两个列表互相独立'''

 输出为:

['charles', 'martina', 'michael', 'florence', 'eli']
['charles', 'martina', 'michael', 'florence', 'eli']
['charles', 'martina', 'michael', 'florence', 'eli', 'eric']
['charles', 'martina', 'michael', 'florence', 'eli', 'jack']

  若不使用切片删去[ : ]),则两个列表会产生关联,无法独立编辑(会同时改变另一个)。

the_players = ['charles', 'martina', 'michael', 'florence', 'eli']
our_players = the_players  '''删去了“[ : ]”'''
print(the_players)
print(our_players)

the_players.append('eric')
our_players.append('jack')
print(the_players)
print(our_players)

 输出变为:

['charles', 'martina', 'michael', 'florence', 'eli']
['charles', 'martina', 'michael', 'florence', 'eli']
['charles', 'martina', 'michael', 'florence', 'eli', 'eric', 'jack']
['charles', 'martina', 'michael', 'florence', 'eli', 'eric', 'jack']

元组

1、元组的概念

  Python将不能修改的值称为不可变的,不可变的列表被称为元组不可修改其中的元素),用括号表示,不同元素间用逗号隔开。

dimensions = (200, 50)    '''定义元组dimensions'''
print(dimensions[0])
print(dimensions[1])  '''打印元组中的元素'''

输出为

200
50

2、遍历元组中的元素

  和列表类似,可用for循环遍历元组中所有值。

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

3、修改元组

  虽然不能直接修改元组中的元素,但是可以通过给存储元组的变量赋值的方式做到这一点,即重新定义整个元组。

dimensions = (200, 50)
print(dimensions)

dimensions = (400, 10)
print(dimensions)

输出为:

(200, 50)
(400, 10)

  相比于列表,元组是更简单的数据结构。如果需要存储的一组值在程序的整个生命周期内都不变,可使用元组。

参考书籍:《Python编程:从入门到实践》

2020-07-08

 

原文地址:https://www.cnblogs.com/carl39/p/13262565.html