列表简介
1.列表是什么
1.1用[ ]来表示列表
smartphone = ['ipone','expensive','nice','swift']
print(smartphone)
#结果 :['ipone','expensive','nice','swift']
1.2访问列表元素
smartphone = ['ipone','expensive','nice','swift']
print(smartphone[0])
#索引从0开始而非1
1.3使用列表元素的值
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = "My first bicycle was a " + bicycles[0].title() + "."
print(message)
2.修改、 添加和删除元素
2.1修改
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
#调出来就行
print(motorcycles)
2.2添加
2.2.1末尾:append()
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')#apped 在此
print(motorcycles)
#结果
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki', 'ducati']
2.22中间插入:insert()
形式 列表名 + insert(索引,“值”)
insert不能作为值去输出
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
#结果
['ducati', 'honda', 'yamaha', 'suzuki']
在这个示例中, 值’ducati’ 被插入到了列表开头 ; 方法insert() 在索引0 处添加空间, 并将值’ducati’ 存储到这个地方。 这种操作将列表中既有的每个元素都右移一个位置:
2.3删除元素
2.3.1del + 列表 + 索引
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
#结果
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']
使用del 删除了列表motorcycles 中的第一个元素——‘honda’
2.3.2使用方法 pop() 删除元素
列表+ .pop(索引)#是有值的
有时候, 你要将元素从列表中删除, 并接着使用它的值。 例如, 你可能需要获取刚被射杀的外星人的 x 和 y 坐标, 以便在相应的位置显示爆炸效果; 在Web应用程序中, 你可能
要将用户从活跃成员列表中删除, 并将其加入到非活跃成员列表中。
方法pop() 可删除列表末尾的元素, 并让你能够接着使用它。 术语弹出 (pop) 源自这样的类比: 列表就像一个栈, 而删除列表末尾的元素相当于弹出栈顶元素。
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
pop()和title()不一样
smartphone = ['ipone','expensive','nice','swift']
smartphone.pop()
print(smartphone)
#result['ipone','expensive','nice']
#看起来是永久操作
print(samrtphone.pop())
#结果
nice
#而不是swift,就是因为第一次的pop把它已经删除最后一个,再一次的smartphone.pop()删除的是nice
pop()是术语弹出的类比
弹出列表中任何位置处的元素
使用pop() 来删除列表中任何位置的元素, 只需在括号中指定要删除的元素的索引即可。
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
print('The first motorcycle I owned was a ' + first_owned.title() + '.')
别忘了, 每当你使用pop() 时, 被弹出的元素就不再在列表中了。
4.根据值删除元素
**列表+. remove(index)#无值的 **
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
#结果
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']
使用remove() 从列表中删除元素时, 也可接着使用它的值。 下面删除值’ducati’ , 并打印一条消息, 指出要将其从列表中删除的原因:
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.1永久排序
按字母顺序排列
**sort()**对列表进行永久性排序
形式:列表名+.sort()
cars = ['brw','audi','toyota','subaru']
cars.sort()
print(cars)
按字母顺序排序的
#结果
['audi', 'bmw', 'subaru', 'toyota']
还可以按与字母顺序相反的顺序排列列表元素, 为此, 只需向sort() 方法传递参数reverse=True 。 下面的示例将汽车列表按与字母顺序相反的顺序排列:
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)
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("\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']
也可向函数sorted() 传递参数reverse=True 。
注意 在并非所有的值都是小写时, 按字母顺序排列列表要复杂些。 决定排列顺序时, 有多种解读大写字母的方式, 要指定准确的排列顺序, 可能比我们这里所做的
要复杂。 然而, 大多数排序方式都基于本节介绍的知识。
倒着打印列表
列表名.reverse()
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
#result
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']
注意, reverse() 不是指按与字母顺序相反的顺序排列列表元素, 而只是反转列表元素的排列顺序:
确定长度len()
len(列表名)
len(列表名)是有值的
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))