- 论坛徽章:
- 4
|
- [liangdong@bb-browser-test00.vm.baidu.com py_project]$ python3 main.py
- [('a|565|+', 1), ('a|890|+', 2), ('b|232|+', 3), ('b|657|+', 4)]
- [liangdong@bb-browser-test00.vm.baidu.com py_project]$ cat main.py
- #python3.2
- #coding=utf8
- import functools
- to_sort = {
- 'a|565|+': 1,
- 'a|890|+': 2,
- 'b|232|+': 3,
- 'b|657|+': 4,
- }
- def compare_key(left, right):
- lfield = left[0].split('|')
- rfield = right[0].split('|')
- if lfield[0] < rfield[0]:
- return -1
- elif lfield[0] > rfield[0]:
- return 1
- else:
- nleft = int(lfield[1])
- nright = int(rfield[1])
- return nleft - nright
- to_sort_iter = iter(to_sort.items())
- after_sort = list(sorted(to_sort_iter, key = functools.cmp_to_key(compare_key)))
- print(after_sort)
复制代码 |
|