Python – 将多尺寸矩阵的后列转换为
当需要转换多尺寸矩阵的后列时,可以使用简单的迭代和“追加”方法以及负向索引。
示例
以下演示了同样的问题 -
my_list = [[41, 65, 25], [45, 89], [12, 65, 75, 36, 58], [49, 12, 36, 98],[47, 69, 78]]
print("The list is : " )
print(my_list)
print("The list after sorting is : " )
my_list.sort()
print(my_list)
my_result = []
for sub_list in my_list:
my_result.append(sub_list[-1])
print("The resultant list is : ")
print(my_result)
print("The list after sorting is : " )
my_result.sort()
print(my_result)输出
The list is : [[41, 65, 25], [45, 89], [12, 65, 75, 36, 58], [49, 12, 36, 98], [47, 69, 78]] The list after sorting is : [[12, 65, 75, 36, 58], [41, 65, 25], [45, 89], [47, 69, 78], [49, 12, 36, 98]] The resultant list is : [58, 25, 89, 78, 98] The list after sorting is : [25, 58, 78, 89, 98]
说明
定义列表并显示在控制台上。
使用“排序”方法进行排序。
创建一个空列表。
迭代列表并访问最后一个元素(使用负向索引)。
将其附加到空列表。
此结果作为控制台上的输出显示。
此列表再次排序并在控制台上显示。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP