1,查列表
bicycles = ['a','','','b']
print(bicycles)
>['a','','','b']
bicycles = ['a','','','b']
print(bicycles[0])
>a不是'a'
bicycles = ['a','','','b']
print(bicycles[0].title)
>A
bicycles = ['a','','','b']
print(bicycles[-1])
>b
2,改列表
bicycles = ['a','','','b']
bicycles[0] = 'c' 不是c (赋值时是字符型‘c’而打印出来的只有c)
print(bicycles)
3,增
bicycles.append('d') 添加到列表末位
bicycles.insert(0,'e') 在0号位添加e
print(bicycles)
4,删
del bicycles[0] 删除0号位的元素,永久删除
print(bicycles)
pop() 删除列表末位的元素,并可继续使用
popped_bicycles = bicycles.pop()
print(bicycles)
print(popped_bicycles)
也可以用pop来删除任何位置的元素
a = bicycles.pop(0)
根据值来删除元素
remove()从列表中删除元素时,也可继续使用它的值
注:
如果要删除一个元素,且不再以任何方式再次使用,则用del()语句。
如果要删除一个元素,但将来还会使用,则使用pop()语句,每当使用pop()语句,被弹出的元素就不在列表中了。
如果要删除的元素不知道其位置,则可以根据值来删除元素,使用remove()语句。如,bicycles.remove('a')
组织列表
1,1 使用sort()对列表进行永久排序,改变列表原始排序
cars.py
cars = ['bmw','audi','toyota']
cars.sort() >按字母顺序排列
cars.sort(reverse=True) >按字母反方向排序
1,2 使用sorted()对列表进行临时排序,不影响列表中的原始排序
sorted(cars) >临时按字母顺序排列
sorted(cars,reverse = True) >临时按字母相反顺序排列
>>>sort() 永久排序 sorted() 临时排序
cars.reverse() >以原始排序的反方向排序
1,3 确定列表长度(len()函数)
len(cars) >>>3
课后题:# 3-1 创建一个包含多个名字的列表 name = ['lily','lilei','xiaomei'] print(name) # 3-2 个性化问候语 name = ['lily','lilei','xiaomei'] print("my favorite name is : " + name[0]) print("my favorite name is : " + name[1]) print("my favorite name is : " + name[2]) # 3-3 自定义列表 diy_list = ['bike','motorcycle'] print("I would like to own a " + diy_list[0]) print("I would like to own a " + diy_list[1]) # 3-4 打印嘉宾名单 name = ['lily','lilei','limei'] print("Looking forward to your visit : " + name[0]) print("Looking forward to your visit : " + name[1]) print("Looking forward to your visit : " + name[2]) # 3-5 修改嘉宾名单 name = ['lily','lilei','limei'] print(name[2] + "will miss out on this invitation.") name = ['lily','lilei','xiaoxiao'] print("Looking forward to your visit : " + name[0]) print("Looking forward to your visit : " + name[1]) print("Looking forward to your visit : " + name[2]) # 3-6 添加嘉宾 name = ['lily','lilei','limei'] print("I have find bigger table") name.insert(0,'lixiao') name.insert(2,'xiaoli') name.append('saisai') print(name) print("Looking forward to your visit : " + name[0]) print("Looking forward to your visit : " + name[1]) print("Looking forward to your visit : " + name[2]) print("Looking forward to your visit : " + name[3]) print("Looking forward to your visit : " + name[4]) print("Looking forward to your visit : " + name[-1]) # 3-7 缩减就餐名单 name = ['lily','lilei','limei'] print("I have find bigger table") name.insert(0,'lixiao') name.insert(2,'xiaoli') name.append('saisai') print(name) print("because something happened, so i can invitation two person only!") for i in range(-4,0): abc = name.pop(i) print(abc + " : I'am sorry, you cannot go to the party") for i in range(0,2): print("Looking forward to your visit : " + name[i]) del name[0:2] print(name) # 3-8 列表排列 city = ['changchun','jinan','shanghai','xi an','changsha'] print(city) # 临时按字母顺序排列此列表 sorted(city) print(city) # 临时按字母相反顺序排列此列表 sorted(city,reverse = True) print(city) # 按列表原始顺序,相反的顺序排列此列表 city.reverse() print(city) # 使用reverse()使得列表恢复到原有排序 city.reverse() print(city) # 永久按字母顺序排列此列表 city.sort() print(city) # 取字母反方向 city.sort(reverse=True) print(city) # 3-9 嘉宾人数 len(name)