第3章 列表简介
在本章中,你学习了:列表是什么以及如何使用其中的元素;如何定义列表以及如何增删元素;如何对列表进行永久性排序,以及如何为展示列表而进行临时排序;如何确定列表的长度,以及在使用列表时如何避免索引错误
目录
2.列表访问元素:索引范围从左到右0——3 从右到左-1—— -4
3.修改append、添加insert和删除del、pop、remove
1.列表定义:
bicycles = ['trek','cannondale','redline''specialized']
print(bicycles)
#数字的表示方法
numbers = [2,3,5,8,5]
2.列表访问元素:索引范围从左到右0——3 从右到左-1—— -4
bicycles = ['trek','cannondale','redline','specialized']
print(bicycles[0].title())
print(bicycles[2].title())
print(bicycles[-3].title())
print(bicycles[-2].title())
print(bicycles[-1].title())
3.修改append、添加insert和删除del、pop、remove
修改:append()将元素添加到列表末尾,不影响列表中其他元素,可以从空列表,使用一系列的append()语句添加元素。
添加:insert()可在列表的任何位置添加新元素,每个元素都右移一个位置
删除:删除元素后还使用元素值选 pop 不使用选del
del可删除任何位置处的列表元素,条件是知道其索引
pop()可删除列表末尾的元素,并让你能够接着使用它(栈顶元素出栈);也可以指定索引
remove() 只知道要删除的元素的值,要删除的值可能在列表中出现多次,就需要使用循环,remove如果要记录删除元素需要先记录后删除
bicycles = ['trek','cannondale','redline','specialized']
print(bicycles[0].title())
print(bicycles[2].title())
print(bicycles[-3].title())
print(bicycles[-2].title())
print(bicycles[-1].title())
#修改
bicycles[0]='math'
print(bicycles)
#末尾添加
bicycles.append('math')
print(bicycles)
#任何位置插入
bicycles.insert(0,'good')
print(bicycles)
#删除---del
del bicycles[0]
print(bicycles)
#删除---pop
popped_bicycle=bicycles.pop()
print(popped_bicycle)
print(bicycles)
popped_bicycle=bicycles.pop(1)
print(popped_bicycle)
print(bicycles)
#删除remove
learn='math'
bicycles.remove(learn)
print(learn)
print(bicycles)
#remove如果要记录删除元素需要先记录后删除
learn=bicycles.remove('redline')
print(bicycles)
print(learn)
4.练习3-4—3-7
print里面如果直接用列表名candidates+字符串的拼接形式会报错
#3-4 print里面如果直接用列表名candidates+字符串的拼接形式会报错
candidates = ['Mike','Jack','Linda']
print(candidates[0]+','+candidates[1]+','+candidates[2]+",may I invite you to dinner with me?")
#3-5 输出不能参加的任命 修改名单 重新输出列表
print(candidates[0]+" will not dinner witn us.")
candidates[0]="蔡依林"
print(candidates[0]+','+candidates[1]+','+candidates[2]+",may I invite you to dinner with me?")
#3-6 不同位置添加新成员
print("Hi!Look!I have found a big dinner table!")
candidates.insert(0,"JJ")
candidates.insert(2,"Jones")
candidates.append("罗志祥")
print(candidates[0]+','+candidates[1]+','+candidates[2]+','+candidates[3]+','+candidates[4]+",may I invite you to dinner with me?")
#3-7
print("Oh,I'm sorry,my new dinner table can not arrive on time!My little table allows only three people to eat dinner,now,I can only need two friends to eat dinner with me!")
#pop删除成员只留下两位
popped_candidate=candidates.pop()
print(popped_candidate+",I'm sorry.")
popped_candidate=candidates.pop()
print(popped_candidate+",I'm sorry.")
popped_candidate=candidates.pop()
print(popped_candidate+",I'm sorry.")
#对留下的人邀请
print(candidates[0]+',we will eat dinner together!')
print(candidates[1]+',we will eat dinner together!')
#del删除表中其他成员
del candidates
#确保表中没有成员
print(candidates)
5.组织列表
排序:
sort()对列表进行永久性排序,sort()方法传递参数reverse=True实现字母反序排序 (假如都是小写)
sorted()对列表进行临时性排序,sorted()方法传递参数reverse=True实现字母反序排序
倒打列表:
reverse()永久性反转列表元素排列顺序(不按字母顺序),再次reverse()就可以恢复原来的顺序
列表长度:
函数len()获取
#cars.py
#sort()永久排序
cars=['bmw','audi','toyota','subru']
cars.sort()
print(cars)
#字母相反顺序 sort()传递参数reverse=True
cars.sort(reverse=True)
print(cars)
#临时排序sorted()
cars=['bmw','audi','toyota','subru']
print("Here is the original list:")
print(cars)
print("Here is the sorted list:")
print(sorted(cars))
print(cars)#检查
#倒打列表
cars=['bmw','audi','toyota','subru']
print("Here is the original list:")
print(cars)
print("Here is the reverse list:")
cars.reverse()
print(cars)
#列表长度
cars=['bmw','audi','toyota','subru']
print("Here is the original list:")
print(cars)
print(len(cars))
6.动手试一试:3-8 3-9 3-10
#3-8放眼世界
locations=['Qingdao','kangaroo','Australia','Beijing','America']
print(locations)
#临时sorted 核实
print(sorted(locations))
print(locations)
#临时sorted字母反序 核实
print(sorted(locations,reverse=True))
print(locations)
#reverse倒打列表 核实
locations.reverse()
print(locations)
#再次reverse恢复 核实
locations.reverse()
print(locations)
#sort字母顺序 核实
locations.sort()
print(locations)
#sort字母反序 核实
locations.sort(reverse=True)
print(locations)
#3-9晚餐嘉宾 共邀请了多少位嘉宾
candidates = ['Mike','Jack','Linda']
print(candidates[0]+','+candidates[1]+','+candidates[2]+",may I invite you to dinner with me?")
print("I have invited "+str(len(candidates))+" friends to eat dinner with me!")
#3-10本章函数全应用 前面的习题已经包含了 不重复了
7.列表索引错误
列表索引从0开始,范围是0——length-1 也可以反向使用索引,最后一个元素的索引既可以是length-1也可以是-1,以此类推
列表为空是出现错误
8.动手试一试3-11
索引错误实例:
索引范围是0——2,3不在范围内
candidates = ['Mike','Jack','Linda']
print(candidates[3])