myfriends=["LinBin","Liupeng","Shang","Zhang"]
message=f"my friends1 is {myfriends[1].title()}"
print(message)
message=f"my last freieds in the list is {myfriends[-1].title()}" #标号-1表示最后一个元素,可用-2,-3,以此类推
print(message)
print(myfriends)
myfriends[1]="Huali"#修改列表元素
message=f"my friends1 is {myfriends[1].title()}"
print(message)
print(myfriends)
myfriends.append("Wh")#在末尾追加
message=f"my friends in the list the last one is {myfriends[-1].title()}"
print(message)
print(myfriends)
myfriends.insert(0,"wt")#在指定位置插入
message=f"my friens0 in the list is {myfriends[1].title()}"
print(message)
print(myfriends)
#使用pop或del删除列表中的元素
del myfriends[0]
message=f"my friens0 in the list is {myfriends[1].title()}"
print(message)
print(myfriends)
poped_friends=myfriends.pop()#删除最后一个元素,不指定参数
message=f"my friens in the list the last is {myfriends[-1].title()}"
print(message)
print(myfriends)
message=f"my friens in the list the last is {poped_friends.title()}"
print(message)
print(myfriends)
poped_friends=myfriends.pop(1)#删除指定元素,指定参数
message=f"my friens in the list the last is {poped_friends.title()}"
print(message)
print(myfriends)
myfriends.remove("LinBin")#按照名称删除
print(myfriends)
myfriends=["LinBin","Liupeng","Shang","Zhang"]
print(myfriends)
print("here is the sorted list:")
print(sorted(myfriends))#按时排序
myfriends.sort();#永久排序
print(myfriends)
myfriends.reverse();#反向
print(myfriends)
print(len(myfriends))#列表长度
#练习
MyDestination=["SanXia","Harbin","Xiamen","Beijing","ChengDu"]
print(MyDestination)
message=f"The Sorted Destination is {sorted(MyDestination)}"
print(message)
print(MyDestination)
print(sorted(MyDestination))
MyDestination.reverse()
print(MyDestination)
MyDestination.reverse()
print(MyDestination)
MyDestination.sort()
print(MyDestination)
print(len(MyDestination))
my friends1 is Liu
my last freieds in the list is Zhang
['Lin', 'Liu', 'Shang', 'Zhang']
my friends1 is Hu
['Lin', 'Hu', 'Shang', 'Zhang']
my friends in the list the last one is Wh
['Lin', 'Hu', 'Shang', 'Zhang', 'Wh']
my friens0 in the list is Lin
['wt', 'Lin', 'Hu', 'Shang', 'Zhang', 'Wh']
my friens0 in the list is Hu
['Lin', 'Hu', 'Shang', 'Zhang', 'Wh']
my friens in the list the last is Zhang
['Lin', 'Hu', 'Shang', 'Zhang']
my friens in the list the last is Wh
['Lin', 'Hu', 'Shang', 'Zhang']
my friens in the list the last is Hu
['Lin', 'Shang', 'Zhang']
['Shang', 'Zhang']
['Lin', 'Liu', 'Shang', 'Zhang']
here is the sorted list:
['Lin', 'Liu', 'Shang', 'Zhang']
['Lin', 'Liu', 'Shang', 'Zhang']
['Zhang', 'Shang', 'Liu', 'Lin']
4
['SanXia', 'Harbin', 'Xiamen', 'Beijing', 'ChengDu']
The Sorted Destination is ['Beijing', 'ChengDu', 'Harbin', 'SanXia', 'Xiamen']
['SanXia', 'Harbin', 'Xiamen', 'Beijing', 'ChengDu']
['Beijing', 'ChengDu', 'Harbin', 'SanXia', 'Xiamen']
['ChengDu', 'Beijing', 'Xiamen', 'Harbin', 'SanXia']
['SanXia', 'Harbin', 'Xiamen', 'Beijing', 'ChengDu']
['Beijing', 'ChengDu', 'Harbin', 'SanXia', 'Xiamen']
5