#3.1什么是列表
#用[]来表示列表,并用逗号来分隔其中元素
bicycles = [‘trek’,‘cannondale’,‘redline’,‘specialized’]
print(bicycles)
#如果让python将列表打印出来,python将打印列表的内部表示,包括括号
#3.1.1访问列表元(告诉python位置)
#从自行车种类中提出第一种自行车
bicycles = [‘trek’,‘cannondale’,‘redline’,‘specialized’]
print(bicycles[0])
#python获取列表时只返回该元素但不包括方括号和引号
#加title元素使得结果更简洁
bicycles = [‘trek’,‘cannondale’,‘redline’,‘specialized’]
print(bicycles[0].title())
#3.1.2索引从0开始而不是1
bicycles = [‘trek’,‘cannondale’,‘redline’,‘specialized’]
print(bicycles[1])
bicycles = [‘trek’,‘cannondale’,‘redline’,‘specialized’]
print(bicycles[3])
#通过将索引定位-1来返回到最后一个
bicycles = [‘trek’,‘cannondale’,‘redline’,‘specialized’]
print(bicycles[-1])
#这种语法非常有用,因为经常需要在不知道列表长度的情况下访问最后一个元素)
#这种约定也适用于其他负数,eg.-2为返回倒数第二个列表元素,-3就是倒数第三个…
#3.1.3使用列表中的各个值
bicycles = [‘trek’,‘cannondale’,‘redline’,‘specialized’]
message = "My first bicycle was a " + bicycles[0].title() + “.”
print(message)
#3.2修改、添加和删除元素
#3.2.1修改列表元素
motorcycles = [‘honda’,‘yamaha’,‘suzuki’]
print(motorcycles)
motorcycles[0] = ‘ducati’
print(motorcycles)
#3.2.2在列表中添加元素
#1.在列表末尾添加元素append()
motorcycles = [‘honda’,‘yamaha’,‘suzuki’]
print(motorcycles)
motorcycles.append(‘ducati’)
print(motorcycles)
#方法append()让动态的创建列表易如反掌,可以先创建空列表,在使用一系列的append()语句添加元素
motorcycles = []
motorcycles.append(‘honda’)
motorcycles.append(‘yamaha’)
motorcycles.append(‘suzuki’)
print(motorcycles)
#2.在列表末中插入元素insert()
#inset()可以在列表中的任何位置添加元素。为此,你需要制定新元素的索引和值
motorcycles = [‘honda’,‘yamaha’,‘suzuki’]
motorcycles.insert(0,‘ducati’)
print(motorcycles)
#3.2.3从列表中删除元素
#1.用del语句删除元素(知道位置)
motorcycles = [‘honda’,‘yamaha’,‘suzuki’]
print(motorcycles)
del motorcycles[0]
print(motorcycles)
#用del语句可删除任何位置的列表元素,条件是知道其索引
motorcycles = [‘honda’,‘yamaha’,‘suzuki’]
print(motorcycles)
del motorcycles[1]
print(motorcycles)
#2.用pop()语句删除末尾元素
#从motorcycles列表中弹出一款摩托车
motorcycles = [‘honda’,‘yamaha’,‘suzuki’]
print(motorcycles)
popped_motorcycles = motorcycles.pop()
print(motorcycles)
print(popped_motorcycles)
#假设摩托车是按照购买时间储存的,就可以用pop()打印一条信息,指出最后购买的是哪款
motorcycles = [‘honda’,‘yamaha’,‘suzuki’]
last_owned = motorcycles.pop()
print("The last motorcycle I owned was a " + last_owned.title() + “.”)
#3.弹出列表中任何位置处的元素
motorcycles = [‘honda’,‘yamaha’,‘suzuki’]
first_owned = motorcycles.pop(0)
print("The first motorcycle I owned was a " + first_owned.title() + “.”)
#注意!使用pop()时被弹出了的元素就不在了
#如果你要从列表中删除一个元素,且不再以任何方式使用它就用del语句;如果你要在删除元素之后还能继续使用就用pop()
#根据值删除元素
#不知道要从列别中删除值的位置,如果只知道删除的元素的值,可使用remove()
motorcycles = [‘honda’,‘yamaha’,‘suzuki’,‘ducati’]
print(motorcycles)
motorcycles.remove(‘ducati’)
print(motorcycles)
#指出将其从列表中删除的原因
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.")
#3.3组织列表
#3.3.1使用方法sort()对列表进行永久性排序(按照字母顺序)
cars = [‘bmw’,‘audi’,‘toyota’,‘subaru’]
cars.sort()
print(cars)
#反向排序,只需要向sort()方法传递参数reverse=True
cars = [‘bmw’,‘audi’,‘toyota’,‘subaru’]
cars.sort(reverse=True)
print(cars)
#3.3.2使用方法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(“Here is the original list again:”)
print(cars)
#调用函数sorted()后,列表元素排列顺序没有变,如果要按与字母顺序相反顺序显示列表,也可以用sorted()传递参数reverse=True
#3.3.3倒着打印列表reverse()
cars = [‘bmw’,‘audi’,‘toyota’,‘subaru’]
print(cars)
cars.reverse()
print(cars)
#此时不在是字母顺序是原本的排列顺序
#reverse()是永久性的,但可以随时恢复排列顺序,只需对列表再次调用reverse()即可
#3.3.4确定列别长度len()
cars = [‘bmw’,‘audi’,‘toyota’,‘subaru’]
len(cars)
4