Python -- List

·

3 min read

什么是列表

列表由一系列按特定顺序排列的元素组成。 你可以创建包含字母表中所有字母、数字0~9或所有家庭成员姓名的列表;也可以将任何东西加入列表中,其中的元素之间可以没有任何关系。

bicycles = ['trek', 'cannondale', 'redline', 'specialized']


创建数值列表

使用range()

numbers = list(range(1,6))

输出结果:

[1, 2, 3, 4, 5]


还可以添加步长

numbers = list(range(2,11,2))

输出结果:

[2, 4, 6, 8, 10]


创建平方列表

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

print(squares)
print(min(squares))
print(max(squares))
print(sum(squares))

输出结果:

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


列表解析

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

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

输出结果:

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

增(在列表中添加元素)

  • append()

在列表末尾添加元素

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

motorcycles.append('ducati')
print(motorcycles)

输出结果:

['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki', 'ducati']


  • insert()

在列表任何位置添加新元素。需要指定新元素的索引和值。

motorcycles = ['honda', 'yamaha', 'suzuki']

motorcycles.insert(0, 'ducati')  
print(motorcycles)

输出结果:

['ducati', 'honda', 'yamaha', 'suzuki']

删(从列表中删除元素)

  • del

如果知道要删除的元素在列表中的位置,可使用del语句。使用del语句将值从列表中删除后,你就无法再访问它了。

motorcycles = ['honda', 'yamaha', 'suzuki']  
print(motorcycles)

del motorcycles[0]
print(motorcycles)

输出结果:

['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']


  • pop()

可以使用pop()来删除列表中任意位置的元素,只需在圆括号中指定要删除元素的索引即可。如果没有指定索引,则删除最后一个元素。

motorcycles = ['honda', 'yamaha', 'suzuki']

first_owned = motorcycles.pop(0)
print(f"The first motorcycle I owned was a {first_owned.title()}.")

输出结果:

The first motorcycle I owned was a Honda.


  • remove()

如果只知道要删除的元素的值,可使用方法remove()。

motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)

motorcycles.remove('ducati')
print(motorcycles)

输出结果:

['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']

Note 方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来确保将每个值都删除。


查(查询列表)

打印列表内部表示和访问列表的任意元素

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
print(bicycles[0])

输出结果:

['trek', 'cannondale', 'redline', 'specialized']
trek


遍历整个列表 用for循环

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

输出结果:

alice
david
carolina


切片 要创建切片,可指定要使用的第一个元素和最后一个元素的索引。与函数range()一样,Python在到达第二个索引之前的元素后停止。要输出列表中的前三个元素,需要指定索引0和3,这将返回索引为0、1和2的元素。

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

输出结果:

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

注意 可在表示切片的方括号内指定第三个值。这个值告诉Python在指定范围内每隔多少元素提取一个。


改(修改元素的值)

修改列表元素,可指定列表名和要修改的元素的索引,再指定该元素的新值。

motorcycles = ['honda', 'yamaha', 'suzuki'] 
print(motorcycles)

motorcycles[0] = 'ducati'
print(motorcycles)

输出结果:

['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']


sort() 对列表按字母顺序进行永久排序。

cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)

输出结果:

['audi', 'bmw', 'subaru', 'toyota']

sort(reverse=True)是按相反方向排序。
sorted() 要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们,可使用函数sorted()。函数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)

输出结果:

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']


reverse() 要反转列表元素的排列顺序,可使用方法reverse()。

cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)

cars.reverse()
print(cars)

输出结果:

['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']

复制列表

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)

输出结果:

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

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

元组

有时候需要创建一系列不可修改的元素,元组可以满足这种需求。Python将不能修改的值称为不可变的,而不可变的列表被称为元组。

定义元组

dimensions = (200, 50)
print(dimensions[0])

print(dimensions[1])

输出结果:

200
50

遍历元组

用for循环,与列表一样。

修改元组变量

虽然不能修改元组的元素,但可以给存储元组的变量赋值。因此,如果要修改前述矩形的尺寸,可重新定义整个元组:

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

dimensions = (400, 100)
print("\nModified dimensions:")  
for dimension in dimensions:
    print(dimension)

输出结果:

Original dimensions:
20050

Modified dimensions:
400100