py_*_*y_9 0 python sorting tuples python-2.7
我有一个像这样的元组列表:
[('peter':1), ('mary':5), ('anthony':6), ('brandon':4)]
Run Code Online (Sandbox Code Playgroud)
如果我想对此列表进行排序并获得类似的内容:
[('anthony':6),('brandon':4),('mary':5),('peter':1)]
Run Code Online (Sandbox Code Playgroud)
我怎么能在python中对它进行排序?
正如Zero指出的那样,您的示例数据无效,但我认为这只是一个打字问题.因此提供数据是有效的,如下所示:
my_data = [('peter',1), ('mary',5), ('anthony',6), ('brandon',4)]
Run Code Online (Sandbox Code Playgroud)
你也可以使用
sorted_data = sorted(my_data)
Run Code Online (Sandbox Code Playgroud)
要么:
my_data.sort()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3997 次 |
| 最近记录: |