使用外部列表对元组进行排序的 Python 程序
如果需要使用外部列表对元组进行排序,可以使用列表解析和‘dict’方法。
以下是对相同内容进行演示:
示例
my_list = [('Mark', 34), ('Will', 91), ('Rob', 23)]
print("The list of tuple is : ")
print(my_list)
ordered_list = ['Will', 'Mark', 'Rob']
print("The ordered list is :")
print(ordered_list)
temp = dict(my_list)
my_result = [(key, temp[key]) for key in ordered_list]
print("The ordered tuple list is : ")
print(my_result)输出
The list of tuple is :
[('Mark', 34), ('Will', 91), ('Rob', 23)]
The ordered list is :
['Will', 'Mark', 'Rob']
The ordered tuple list is :
[('Will', 91), ('Mark', 34), ('Rob', 23)]说明
元组列表已定义,并在控制台上显示。
定义另一个列表并在控制台上显示。
将元组列表转换成一个字典。
对其进行迭代并将元素存储在另一个列表中。
这将生成一个有序元组,作为输出显示在控制台上。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP