python编程基础知识—列表(二)

时间:2021-12-27 08:37:45

3操作列表

3.1 遍历整个列表

  • 使用for循环
cars = ['bmw','audi','toyota','Jeep']
for i in cars:
print(i)
bmw
audi
toyota
Jeep

3.1.1 在for循环中执行更多的操作

cars = ['bmw','audi','toyota','Jeep']
for i in cars:
print(i + " was my first car" + '!' )
bmw was my first car!
audi was my first car!
toyota was my first car!
Jeep was my first car!
cars = ['bmw','audi','toyota','Jeep']
for i in cars:
print(i + " was my first car" + '!' )
print("my first car was, " + i + ".\n")
bmw was my first car!
my first car was, bmw. audi was my first car!
my first car was, audi. toyota was my first car!
my first car was, toyota. Jeep was my first car!
my first car was, Jeep.

3.1.2 在for循环结束后执行一些操作

cars = ['bmw','audi','toyota','Jeep']
for i in cars:
print(i + " was my first car" + '!' )
print("my first car was, " + i + ".\n")
print("Thanks for everyone! That's a good idea.")
bmw was my first car!
my first car was, bmw. audi was my first car!
my first car was, audi. toyota was my first car!
my first car was, toyota. Jeep was my first car!
my first car was, Jeep. Thanks for everyone! That's a good idea.

3.2 创建数值列表

3.2.1 使用函数range()

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

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

  • 可使用函数list()将range()的结果直接转换为列表。若将range()作为list()的参数,则会输出一个数字列表.
numbers = list(range(1,6))
print(numbers)
[1, 2, 3, 4, 5]
dual_numbers = list(range(2,13,2))#range(2,13,2)第一个数为起始值,第二个为终值,第三个为步长
print(dual_numbers)
[2, 4, 6, 8, 10, 12]
A = []
for i in range(5):
B = i**2
A.append(B)
print(A)
[0, 1, 4, 9, 16]

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

B = [1,2,3,5,4,9,8,6]
print(min(B))#求最小
print(max(B))#求最大
print(sum(B))#求和
1
9
38
B = [i ** 2 for i in range(5)]
print(B)
[0, 1, 4, 9, 16]

3.2 使用列表的一部分

3.2.1 切片

cars = ['bmw','audi','toyota','Jeep']
print(cars[0:2])
print(cars[1:3])
print(cars[2:4])
print(cars[:3])
print(cars[1:])
print(cars[:])
print(cars[-2:])
['bmw', 'audi']
['audi', 'toyota']
['toyota', 'Jeep']
['bmw', 'audi', 'toyota']
['audi', 'toyota', 'Jeep']
['bmw', 'audi', 'toyota', 'Jeep']
['toyota', 'Jeep']

3.2.2 遍历切片

cars = ['bmw','audi','toyota','Jeep']
print("This is the first four types in my car:")
for i in cars[:4]:
print(i)
This is the first four types in my car:
bmw
audi
toyota
Jeep

3.2.3 复制列表

  • 要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引([:]),
pizza = ['bread','hotdog','milk','egg','cake']
food = pizza[:]
print("My favorite food is:")
print(pizza)
print("\nMy friend's favorite food was:")
print(food)
My favorite food is:
['bread', 'hotdog', 'milk', 'egg', 'cake'] My friend's favorite food was:
['bread', 'hotdog', 'milk', 'egg', 'cake']
pizza = ['bread','hotdog','milk','egg','cake']
food = pizza[:]
pizza.append('ice cream')
food.append('cannoli')
print("My favorite food is:")
print(pizza)
print("\nMy friend's favorite food was:")
print(food)
My favorite food is:
['bread', 'hotdog', 'milk', 'egg', 'cake', 'ice cream'] My friend's favorite food was:
['bread', 'hotdog', 'milk', 'egg', 'cake', 'cannoli']

列表方法说明

方法 说明
L.append(v) 将值v添加到列表L的末尾
L.insert(i,v) 将值v添加到索引i处,同时将其后的元素往后移以便腾出位置
L.remove(v) 从列表L中移除第一次找到的v值
L.reverse() 将列表L中的值逆序放置
L.sort() 对列表L中的值以升序排列(字符串以字母表顺序为主)
L.pop() 移除并返回列表中的最后一个元素(该列表不得为空)

4 嵌套列表

  • 也就是说,列表中也可以含有列表,就像循环中能有另一个循环一样.

    如,下面这个嵌套列表描述的是不同国家人口的预期寿命:
[['Canada',76.5],['United States',75.5],['Mexico','72.0']]
[['Canada', 76.5], ['United States', 75.5], ['Mexico', '72.0']]
  • 外层列表的每个元素本身就是一个拥有两个元素的列表,我们可通过标准的标记法来访问外层列表中的元素
life = [['Canada',76.5],['United States',75.5],['Mexico','72.0']]
life[0]
['Canada', 76.5]
life[1]
['United States', 75.5]
life[2]
['Mexico', '72.0']
life[1][0]
'United States'
life[1][1]
75.5
  • 我们还可以将子列表赋值给变量
life = [['Canada',76.5],['United States',75.5],['Mexico','72.0']]
Canada = life[0]
Canada
['Canada', 76.5]
Canada[0]
'Canada'
  • 通过这个引用对子列表做出的任何修改都将反映到主列表中,反之亦然
life = [['Canada',76.5],['United States',75.5],['Mexico','72.0']]
Canada = life[0]
Canada[1]=80.0
Canada
['Canada', 80.0]
life
[['Canada', 80.0], ['United States', 75.5], ['Mexico', '72.0']]