python进阶(一):列表

概要:
python进阶(一):列表

一、列表简介

1、列表是什么?

列表 由一系列按特定顺序排列的元素组成。python中用方括号[]来表示列表,其中的元素之间可以没有任何关系。
例如:

colors = ['yellow', 'red', 'black', 'white', 'purple'] 
print(colors)
##### result #####
['yellow', 'red', 'black', 'white', 'purple']

访问列表元素
列表是有序集合,要访问列表的任何元素,只需要将该元素的位置或者索引告诉python即可。

colors = ['yellow', 'red', 'black', 'white', 'purple']
# 访问列表中的第一个元素
print(colors[0])
# 将输出的元素首字母大写
print(colors[0].title())
##### result #####
yellow
Yellow

注意:列表中的索引是从0开始。
访问列表中的最后一个元素可将其索引指定为-1

colors = ['yellow', 'red', 'black', 'white', 'purple']
# 访问列表中的最后一个元素
print(colors[-1])
##### result #####
purple

这种语法很有用,可以在不需要知道列表的长度的情况下访问最后一个元素。同理,倒数第二个元素可用索引-2来访问,以此类推…
使用列表中的各个值
通过访问列表中的值,创建消息

colors = ['yellow', 'red', 'black', 'white', 'purple']
message = 'My favourite color is ' + colors[-1] + '.'
print(message)
##### result #####
My favourite color is purple.

2、修改、添加和删除元素

修改列表元素
与访问列表元素的语法类似,要修改列表元素,先要指定要修改的列表名和要修改元素的索引,再指定该元素的新值。

colors = ['yellow', 'red', 'black', 'white', 'purple']
print(colors)
# 修改列表中的第一个元素
colors[0] = 'orange'
print(colors)
##### result #####
['yellow', 'red', 'black', 'white', 'purple']
['orange', 'red', 'black', 'white', 'purple']

在列表中添加元素
方法append() 可在列表末尾添加一个新的元素,也可通过此方法动态的创建一个新的列表

colors = ['yellow', 'red', 'black', 'white', 'purple']
colors.append('orange')
print(colors)

bicycles = []
bicycles.append('trek')
bicycles.append('cannondale')
bicycles.append('redline')
print(bicycles)
##### result #####
['yellow', 'red', 'black', 'white', 'purple', 'orange']
['trek', 'cannondale', 'redline']

方法insert()可在列表的任何位置添加新元素,只需指定要插入新元素的索引和值

bicycles = ['trek', 'cannondale', 'redline']
bicycles.insert(0,  'specialized')
print(bicycles)
##### result #####
['specialized', 'trek', 'cannondale', 'redline']

删除列表中的元素
使用del语句可删除列表中元素的值

bicycles = ['trek', 'cannondale', 'redline']
del bicycles[0]
print(bicycles)
##### result #####
['cannondale', 'redline']

使用pop()方法可删除列表末尾的元素,并能接着使用其值

motorcycles = ['honda', 'yamaha', 'suzuki'] 
print(motorcycles) 
popped_motorcycle = motorcycles.pop() 
print(motorcycles) 
print(popped_motorcycle)
##### result #####
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
suzuki

使用pop()方法也可删除列表中任何位置的元素,只需要指定删除元素的索引即可。

motorcycles = ['honda', 'yamaha', 'suzuki'] 
motorcycles.pop(1)
print(motorcycles)
##### result #####
['honda', 'suzuki']

如果不知道删除元素在列表中的位置,只知道删除元素的值,可用remove()方法

motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.remove('honda')
print(motorcycles)
##### result #####
['yamaha', 'suzuki']

3、组织列表

使用sort()方法对列表进行永久性的排序

cars = ['bmw', 'audi', 'toyota', 'subaru'] 
cars.sort() 
print(cars)
##### result #####
['audi', 'bmw', 'subaru', 'toyota']
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)
##### result #####
['toyota', 'subaru', 'bmw', 'audi']

函数sorted()对列表进行临时排序,能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排 列顺序。

cars = ['bmw', 'audi', 'toyota', 'subaru'] 
print("Here is the original list:") 
print(cars)
print("\nHere is the sorted list:") 
print(sorted(cars)) 
print("\nHere is the original list again:") 
print(cars)
##### result #####
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']

Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']

倒着打印列表

cars = ['bmw', 'audi', 'toyota', 'subaru'] 
print(cars) 
cars.reverse() 
print(cars)
##### result #####
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']

确定列表的长度

cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))
##### result #####
4

二、操作列表

1、遍历列表

用for循环遍历列表中的所有元素,对每个元素执行相同的操作

magicians = ['alice', 'david', 'carolina']
for magician in magicians: 
    print(magician)
##### result #####
alice
david
carolina
magicians = ['alice', 'david', 'carolina'] 
for magician in magicians: 
    print(magician.title() + ", that was a great trick!") 
    print("I can't wait to see your next trick, " + magician.title() + ".\n")
print("Thank you, everyone. That was a great magic show!")
##### result #####
Alice, that was a great trick!
I can't wait to see your next trick, Alice.

David, that was a great trick!
I can't wait to see your next trick, David.

Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.

Thank you, everyone. That was a great magic show!

2、创建数值列表

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

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

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

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

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

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

3、使用列表中的一部分

切片:指定使用的第一个元素和最后一个元素的索引

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

若没有指定第一个索引,自动从列表的第一个值开始提取;

players = ['charles', 'martina', 'michael', 'florence', 'eli'] 
print(players[1:4])
print(players[:4])
print(players[3:])
##### result #####
['martina', 'michael', 'florence']
['charles', 'martina', 'michael', 'florence']
['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())
##### result #####
Here are the first three players on my team:
Charles
Martina
Michael

复制列表:根据既有列表创建一个新的列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引[:]

my_foods = ['pizza', 'falafel', 'carrot cake'] 
friend_foods = my_foods[:] 
print("My favorite foods are:") 
print(my_foods) 
print("\nMy friend's favorite foods are:") 
print(friend_foods)
##### result #####
My favorite foods are:
['pizza', 'falafel', 'carrot cake']

My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake'

4、元组

元组:不可变的列表。python中用圆括号()来表示,其操作和列表类似。

dimensions = (200, 50)
print(dimensions[0]) 
print(dimensions[1])
##### result #####
200
50

元组的元素是不可修改的
python进阶(一):列表
遍历元组中的值:for循环

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

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

dimensions = (200, 50) 
print("Original dimensions:") 
for dimension in dimensions: 
    print(dimension)
    
dimensions = (400, 100)
print("\nModified dimensions:") 
for dimension in dimensions: 
    print(dimension)
##### result #####
Original dimensions:
200
50

Modified dimensions:
400
100

over!!!!
python进阶(一):列表

上一篇:Flutter控件---超简单的模糊阴影实现


下一篇:C# Modbus_TCP通讯 dll库 测试TRIO运动控制器