python学习之列表

本文详细介绍了Python中对列表进行操作的方法,包括修改元素值、在列表末尾追加、指定位置插入、删除元素(pop和remove)以及列表的排序、反转和获取长度等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值