概述
- 列表是由一系列按特定顺序排列的元素组成。可以创建包含字母表中的所有字母、数字0-9或所有家庭成员姓名的列表;也可以将任何东西加入列表,其中的元素之间可以没有任何关系。给列表指定一个复数的名字是个不错的选择。
- 在python中用方括号[]来表示列表,并用逗号来分割其中的元素。
- 访问列表元素
列表是有序集合,因此要访问列表的任何元素,需要知道元素的位置或索引。要访问元素,指出列表名称和元素的索引,并放在[]内即可
bicycles = ['trek','cannondale','redline','specialized']
print(bicycles[0])
运行结果:
trek
- 索引从0开始,python为访问最后一个元素提供了另一种方法,将索引指定为-1即可
bicycles = ['trek','cannondale','redline','specialized']
print(bicycles[-1])
运行结果如下:
specialized
- 使用列表中的值
bicycles = ['trek','cannondale','redline','specialized']
message = "my first bicycle was a " + bicycles[0].title() + "."
print(message)
运行结果如下:
my first bicycle was a Trek.
- 修改、添加、删除元素
一、 修改列表元素
- 指定列表名,要修改的元素的索引,指定改元素的新值
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
运行结果如下:
[‘honda’, ‘yamaha’, ‘suzuki’]
[‘ducati’, ‘yamaha’, ‘suzuki’]
二、添加元素
- 在列表末尾添加元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
- 在列表中插入元素
motorcycles = ['honda','yamaha','suzuki']
#在索引0处添加空间,将值‘ducati’添加到这个位置
motorcycles.insert(0,'ducati')
print(motorcycles)
运行结果:[‘ducati’, ‘honda’, ‘yamaha’, ‘suzuki’]
注:inster()方法需要指定索引和值
三、从列表中删除元素
- 用del语句删除元素(如果知道删除的元素的位置,可使用del语句)
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
运行结果如下:
[‘honda’, ‘yamaha’, ‘suzuki’]
[‘yamaha’, ‘suzuki’]
2.用pop()方法删除元素
注:如果删除元素还想接着使用它的值,则可以选择pop方法,pop可以删除列表末尾的元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
#方法pop()可以打印最后一条信息
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
运行结果如下:
[‘honda’, ‘yamaha’, ‘suzuki’]
[‘honda’, ‘yamaha’]
suzuki
[‘honda’]
yamaha
实际上pop()可以删除列表中任何位置的元素,只需要在括号中指定要删除的元素的索引即可
motorcycles = ['honda','yamaha','suzuki']
first_owned = motorcycles.pop(0)
print("the first motorcycle i owned was a " + first_owned.title() + '.' )
运行结果:
the first motorcycle i owned was a Honda.
注:
如果从列表删除一个元素,且不以任何形式使用,就用del语句
如果删除元素后还能使用,则用pop方法
3.根据值删除元素
有的时候不知道要从列表中删除的元素的位置,如果只知道元素的值可以使用remove()
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
motorcycles.remove('yamaha')
print(motorcycles)
运行结果:
[‘honda’, ‘yamaha’, ‘suzuki’]
[‘honda’, ‘suzuki’]
注:
- 使用remove方法时删除元素也可以接着使用它的值
motorcycles = ['honda','yamaha','suzuki','ducati']
print(motorcycles)
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print("nA " + too_expensive.title() + "is too expensive for me.")
运行结果:
[‘honda’, ‘yamaha’, ‘suzuki’, ‘ducati’]
[‘honda’, ‘yamaha’, ‘suzuki’]
A Ducatiis too expensive for me.
- 方法remove只删除第一个指定的值,如果要删除的元素可能在列表中出现多次,就需要用循环来判断是否删除了所有这样的值
四、组织列表
1.使用sort方法对列表进行永久性排序
cars = ['bmw','audi','toyota','subaru']
cars.sort()
print(cars)
#字母相反的顺序排序
cars = ['bmw','audi','toyota','subaru']
cars.sort(reverse = True)
print(cars)
运行结果:
[‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
[‘toyota’, ‘subaru’, ‘bmw’, ‘audi’]
2.使用sorted方法进行临时排序
要保留列表原有的排列顺序,同时以特定的顺序呈现他们,可以使用sorted方法
cars = ['bmw','audi','toyota','subaru']
print("这是排序之前的顺序:")
print(cars)
print("这是排列之后的顺序:")
print(sorted(cars))
print("这是原始顺序:")
print(cars)
运行结果:
这是排序之前的顺序:
[‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
这是排列之后的顺序:
[‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
这是原始顺序:
[‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
3.倒着打印列表
cars = ['bmw','audi','toyota','subaru']
print(cars)
cars.reverse()#reserve永久修改列表顺序,但是再次调用reserve则可恢复原样
print(cars)
运行结果:
[‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
[‘subaru’, ‘toyota’, ‘audi’, ‘bmw’]
五、操作列表
1、遍历整个列表
需要对列表每个元素执行相同的操作,可以使用python中的for循环
magicians = ['alice','david','carolina']
for name in magicians:
print(name)
运行结果:
alice
david
carolina
2、创建数值列表
(1)使用range函数
for value in range(1,5):
print(value)
运行结果:
1
2
3
4
(2)使用range创建数字列表
numbers = list(range(1,6))
print(numbers)
运行结果:
[1, 2, 3, 4, 5]
注:使用range函数时可以指定步长
even_numbers = list(range(2,11,2))#2-11,不断加2,直到到达或超过最终值
print(even_numbers)
运行结果:
[2, 4, 6, 8, 10]
3、列表解析
squares = []
for values in range(1,11):
square = values ** 2
squares.append(square)
print(squares)
运行结果:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
列表解析将for循环和创建新元素的代码合成一行
squares = [value ** 2 for value in range(1,11)]
print(squares)
运行结果:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
4、切片
(1)
#可指定要使用的第一个元素和最后一个元素的索引
players = ['charles','martina','michael','florence','eli']
print(players[0:3])
print(players[1:4])
#如果没指定第一个索引,将自动从头开始
print(players[:4])
print(players[2:])
运行结果:
[‘charles’, ‘martina’, ‘michael’]
[‘martina’, ‘michael’, ‘florence’]
[‘charles’, ‘martina’, ‘michael’, ‘florence’]
[‘michael’, ‘florence’, ‘eli’]
无论列表多长,都会输出从从特定位置到列表末尾的所有元素
(2)遍历切片
players = ['charles','martina','michael','florence','eli']
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())
运行结果:
Here are the first three players on my team:
Charles
Martina
Michael
(3)复制列表
要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:])
my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]
print("my favorite foods are:")
print(my_foods)
print("nmy friend's favourite foods are:")
print(friend_foods)
运行结果:
my favorite foods are:
[‘pizza’, ‘falafel’, ‘carrot cake’]
my friend’s favourite foods are:
[‘pizza’, ‘falafel’, ‘carrot cake’]
最后
以上就是自觉小笼包为你收集整理的列表的全部内容,希望文章能够帮你解决列表所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复