python学习之列表

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
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要对Python列表中的元素进行降序排序,可以使用sort()方法并传递reverse=True参数。例如: ``` my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] my_list.sort(reverse=True) print(my_list) ``` 输出结果为: ``` [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1] ``` 这样就可以将列表中的元素按照从大到小的顺序进行排序。 ### 回答2: 在Python中,列表是一种非常重要的数据类型,也是我们在编写程序中经常使用的数据结构之一。列表中存储的可以是任意类型的数据,包括数字、字符串、布尔值等等。在列表中,我们可以根据需要添加、删除、修改和排序其中的元素。 降序排序是指按照从大到小的顺序对列表中的元素进行排序。Python内置的sort()函数可以实现列表按照升序排序,但是如果我们需要降序排序,就需要使用另一种方式,即reverse=True参数。下面是一个示例代码,演示了如何对一个列表进行降序排序: ```python numbers = [3, 8, 1, 6, 2] numbers.sort(reverse=True) print(numbers) ``` 在上面的代码中,我们定义了一个名为numbers的列表,其中包含五个数字。然后我们使用sort()函数对这个列表进行排序,同时传递了一个reverse=True参数,表示按照降序排序。最后我们使用print()函数输出了排序后的列表。执行这段代码后,你会得到如下所示的输出: ```python [8, 6, 3, 2, 1] ``` 可以看到,按照从大到小的顺序,列表中的数字已经被正确排序了。需要注意的是,sort()函数会修改原始列表,因此如果你需要保留原始列表的顺序,可以先将其复制到一个新的列表中,再对新列表进行排序。另外,如果你需要按照多个条件对列表进行排序,可以使用sort()函数的key参数,具体使用方式可以参考Python的官方文档。 综上所述,学习列表的应用之后,降序排序是非常重要的技能。掌握了降序排序的方法,我们就可以轻松对列表、元组、字典等数据结构进行排序,并根据需要进行添加、删除和修改操作,提高代码的可读性和运行效率。 ### 回答3: Python中的列表(list)是一种非常常用的数据结构,可以容纳任何类型的数据,并且可以进行各种操作。在数据处理和算法中,对列表进行排序是一个非常常见的操作,Python提供了多种排序方法,其中之一就是降序排序。 在Python中,可以使用sort()函数对列表进行排序。sort()函数有两个可选参数key和reverse。其中key是一个函数,用于生成排序的依据,如果不指定,默认按照元素的大小进行排序。而reverse用于指定排序的是升序还是降序,默认为False,即升序排序。如果要进行降序排序,只需要将reverse设置为True即可。 以下是一个简单的例子,说明如何使用sort()函数进行降序排序: ``` nums = [1, 5, 3, 8, 2] nums.sort(reverse=True) print(nums) ``` 运行结果为: ``` [8, 5, 3, 2, 1] ``` 上述代码中,我们首先定义了一个包含5个整数的列表nums。然后使用sort()函数对其进行降序排序,并将reverse参数设置为True。最后输出排序后的列表。 除了sort()函数之外,Python还提供了其他的排序函数,例如sorted()函数可以对任何可迭代的对象进行排序,而不仅仅是列表。此外,还可以使用lambda表达式指定排序依据,例如按照列表中每个元素的第二个值进行排序: ``` items = [(1, 2), (3, 1), (5, 4), (0, 2)] items.sort(key=lambda x: x[1], reverse=True) print(items) ``` 运行结果为: ``` [(5, 4), (1, 2), (0, 2), (3, 1)] ``` 上述代码中,我们定义了一个包含4个元组的列表items,每个元组包含两个整数。然后使用sort()函数进行排序,指定排序依据为每个元组的第二个值,即使用lambda表达式生成排序依据。最后输出排序后的列表。 总之,Python中的列表及其排序方法,在数据处理和算法中非常常用。掌握列表的基本操作,以及sort()和sorted()函数的使用方法,可以帮助我们更加高效地处理数据和编写代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值