列表:包含多个元素,用([ ])表示列表,用逗号分隔其中的元素
bycicles = ['scd', 'asda'] print(bycicles)
访问列表元素,列表名[0].tittle()表示访问列表中第一个元素,并使该元素第一个字母大写
print(bycicles[0].title())
Python的索引是从0开始的,最后一个列表元素的索引指定为-1,倒数第二个为-2,同理以此类推倒数第三个为-3
bicycles=['trek','cannondale','redline','specialized'] message=f"My first bicycle was a {bicycles[-1].title()}" print(message)
修改列表元素:
motorcycles=['honda','yamaha','suzuki'] print(motorcycles) motorcycles[0]='duzati' print(motorcycles)
在列表中添加元素:append()
motorcycles=['honda','yamaha','suzuki'] print(motorcycles) motorcycles.append('duzati') print(motorcycles)
创建空列表用于存储用户将要输入的值:
motorcycles=[] motorcycles.append('duzati') motorcycles.append('honda') motorcycles.append('suzuki') print(motorcycles)
在列表中插入元素:insert()
motorcycles=['honda','yamaha','suzuki'] print(motorcycles) #在位置0处出入duzati motorcycles.insert(0,'duzati') print(motorcycles)
从列表中删除元素:
①del()
motorcycles=['honda','yamaha','suzuki'] print(motorcycles) del motorcycles[1] print(motorcycles)
②用pop()方法删除元素
motorcycles=['honda','yamaha','suzuki'] print(motorcycles) popped_motorcycle=motorcycles.pop() print(motorcycles) print(popped_motorcycle)
该方法删除列表末尾的元素,并让你能够接着使用它,列表就像一个栈,删除列表末尾的元素相当于弹出栈顶元素
③删除列表中任意位置的元素 pop()
motorcycles=['honda','yamaha','suzuki'] print(motorcycles) popped_motorcycle=motorcycles.pop(0) print(motorcycles) print(popped_motorcycle) print(f"The first motorcycle I owned was a {popped_motorcycle.title()}")
可以使用pop()删除列表中任意位置的元素,只需要在括号中指定要删除的元素的索引即可
如果要从列表中删除一个元素,且不再以任何方式使用它,就使用del语句,如果要在删除元素后继续使用它,就使用pop()方法
④根据值删除元素
motorcycles=['honda','yamaha','suzuki'] print(motorcycles) motorcycles.remove('honda') print(motorcycles)
使用remove()从列表删除元素后,也可继续使用它的值:
motorcycles=['honda','yamaha','suzuki'] print(motorcycles) too_expensive='honda' motorcycles.remove('honda') print(motorcycles) print(f"\nA {too_expensive.title()} is too expensive for me ")
tip:remove()方法只删除第一个指定的值,如果要删除的值可能在列表中出现多次,就需要使用循环,确保每个值都删除
3.2.2作业
names = ['zhy', 'zwn', 'lj'] print(f"{names[0]},你好,明天晚上有空一起吃饭吗?") print(f"{names[1]},你好,明天晚上有空一起吃饭吗?") print(f"{names[-1]},你好,明天晚上有空一起吃饭吗?") print(f"{names[2]}无法按时赴约") names[2]='hl' # 把lj替换成hl names.insert(0,'ljl') names.insert(2,'ljw') names.insert(5,'wsy') print(names) print(f"{names[0]},你好,明天晚上有空一起吃饭吗?") print(f"{names[1]},你好,明天晚上有空一起吃饭吗?") print(f"{names[2]},你好,明天晚上有空一起吃饭吗?") print(f"{names[3]},你好,明天晚上有空一起吃饭吗?") print(f"{names[4]},你好,明天晚上有空一起吃饭吗?") print(f"{names[5]},你好,明天晚上有空一起吃饭吗?") print('非常抱歉,由于新买的餐桌无法及时送达吗,我只能邀请两位嘉宾共进晚餐') popped_parted=names.pop() print(f"{popped_parted},非常抱歉,我无法邀请您参加聚餐") popped_parted=names.pop() print(f"{popped_parted},非常抱歉,我无法邀请您参加聚餐") popped_parted=names.pop() print(f"{popped_parted},非常抱歉,我无法邀请您参加聚餐") popped_parted=names.pop() print(f"{popped_parted},非常抱歉,我无法邀请您参加聚餐") print(f"{names[0]},{names[1]},您两位还在被邀请之列") del names[0] del names[0] print(names)
sort()方法对列表进行永久排序
cars.sort() # 使得列表元素按照字母先后排序
cars.sort(reverse=True) # 使得列表元素按照字母顺序相反的顺利额排列元素
永久排序表示再也无法恢复到原来的排列顺序
sorted()函数对列表进行临时排序 sorted(cars)
friends=['abc','rrz','ffa','edf'] print(friends) print(sorted(friends)) print(friends)
print(sorted(friends,reverse=True)) print(friends)
在并非所有的值都是全小写时,按字母顺序排列列表要复杂一些,在确定排列顺序时,有多种解读大写字母的方式,此时要指定准确的排列方式,可能会比以上内容更加复杂。
反向打印列表
cars.reverse()
reverse()方法会永久修改列表元素的排列顺序,但可以随时恢复到原来的排列顺序,只需要对列表再次调用reverse()即可。
确定列表的长度
len(cars)
Python在计算列表元素数时从1开始,因此你在确定列表长度时不会遇到差一误差