python学习之路(二)-列表简介

学习来源于《Python编程:从入门到实践》,感谢本书的作者和翻译工作者。

下面为学习笔记

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles) #将打印出 整个列表内容 ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0]) #打印出 trek

# Python为访问最后一个列表元素提供了一种特殊语法。
# 通过将索引指定为-1,可让Python返回最后一个列表元素
print(bicycles[-1])

# 这种约定也适用于其他负数索引,例如,索引-2返回倒数第二个列表元素,
# 索引-3返回倒数第三个列表元素,以此类推
print(bicycles[-2])

# 修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0]='ducati'
print(motorcycles)
# 在列表中添加元素
# 1. 在列表末尾添加元素
motorcycles.append('ducati')
print(motorcycles) #方法append()将元素'ducati'添加到了列表末尾

# 方法append()让动态地创建列表易如反掌,例如,你可以先创建一个空列表,再使用一系列的
# append()语句添加元素。下面来创建一个空列表,再在其中添加元素'honda'、 'yamaha'和'suzuki'
motorcycles1 = []
motorcycles1.append('honda')
motorcycles1.append('yamaha')
motorcycles1.append('suzuki')
print(motorcycles1)
# 2. 在列表中插入元素
# 使用方法insert()可在列表的任何位置添加新元素。为此,你需要指定新元素的索引和值。
motorcycles2 = ['honda', 'yamaha', 'suzuki']
motorcycles2.insert(0, 'ducati')
print(motorcycles2)
motorcycles2.insert(1,'kawasaki')
print(motorcycles2)
# 从列表中删除元素
# 1. 使用del语句删除元素
del motorcycles2[0]
print(motorcycles2)
# 使用方法pop()删除元素
# 方法pop()可删除列表末尾的元素,并让你能够接着使用它。术语弹出( pop)源自这样的类
# 比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。
motorcycles3 = ['honda', 'yamaha', 'suzuki']
print(motorcycles3)
popped_motorcycle = motorcycles3.pop()
print(motorcycles3)
print(popped_motorcycle)
# 3. 弹出列表中任何位置处的元素实际上,你可以使用pop()来删除列表中任何位置的元素,
# 只需在括号中指定要删除的元素的索引即可。
first_owned = motorcycles2.pop(0)
print('The first motorcycle I owned was a ' + first_owned.title() + '.')
# 4. 根据值删除元素
# 有时候,你不知道要从列表中删除的值所处的位置。如果你只知道要删除的元素的值,可使
# 用方法remove()。
motorcycles4 = ['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles4.remove('ducati')
print(motorcycles4) #方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,
# 就需要使用循环来判断是否删除了所有这样的值。

#作业
#3-4 嘉宾名单
friends = ['GUO','DU','TAO']
print(friends)
# 3-5 修改嘉宾名单:你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。
friends[0]='ZHANG'
print(friends)
# 3-6 添加嘉宾:你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀
# 请哪三位嘉宾。
friends.insert(0,'GUO')
friends.insert(2,'LU')
friends.append('XU')
print(friends)
# 3-7 缩减名单:你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。
friends_out=[]
friends_out.append(friends.pop())
friends_out.append(friends.pop())
friends_out.append(friends.pop())
friends_out.append(friends.pop())
print(friends)
print(friends_out)

# 使用方法 sort()对列表进行永久性排序
# Python方法sort()让你能够较为轻松地对列表进行排序。假设你有一个汽车列表,并要让其
# 中的汽车按字母顺序排列。为简化这项任务,我们假设该列表中的所有值都是小写的。
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
# 你还可以按与字母顺序相反的顺序排列列表元素,为此,只需向sort()方法传递参数
#  reverse=True
cars.sort(reverse=True)
print(cars)
# 要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们,可使用函数sorted()。函数
# sorted()让你能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序。
cars1=['bmw','audi','toyota','subaru']
cars1.append('honda')
cars1.append('volkswagen')
cars1.append('benz')
print(cars1)
print(sorted(cars1))
print(cars1)
cars2=sorted(cars1)
print(cars2)
# 要反转列表元素的排列顺序,可使用方法reverse(),永久性的,只翻转列表
cars1.reverse()
print(cars1)
# 使用函数len()可快速获悉列表的长度
print(len(cars1))

猜你喜欢

转载自blog.csdn.net/mugong11/article/details/81165360