Printing a Dictionary Items Sorted by Its Keys
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Company(object): def __init__(self, name): self.name = name self.employees = {} def addEmployee(self, id, name): self.employees[id] = name def displayEmployees(self): tmp = [ (k,v) for k,v in self.employees.items() ] tmp.sort() for k,v in tmp: print(k, '\t', v) a = Company('The Company') a.addEmployee(111, 'Employee1') a.addEmployee(222, 'Employee2') a.addEmployee(333, 'Employee3') a.displayEmployees() |
有没有另一种方法可以按字典的键对其进行排序,以便在不使用新变量的情况下保持以下输出?:
1 2 3 | 111 Employee1 222 Employee2 333 Employee3 |
对于大量员工,近乎最佳的可能是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Company(object): def __init__(self, name): self.name = name self.employee_ids = [] self.employees = {} self.sorted = True def addEmployee(self, id, name): self.employee_ids.append(id) self.employees[id] = name self.sorted = False def displayEmployees(self): if not self.sorted: self.employee_ids.sort() self.sorted = True for k in self.employee_ids: print k, '\t', self.employees[k] |
这就需要
"有效爪哇"名声的Josh Bloch,当时是一名谷歌员工,在一个技术演讲中,展示了Python的Tim排序,然后隐喻地说:——在大马士革的路上被闪电击中——拔出他的笔记本电脑(我记得我们都坐在前排)开始黑客攻击。不久之后,TimSope成为Java对对象数组排序的方式(唉,不是一个原语数组),因为技术上的原因,它必须保持"快速排序"的一个不太健壮的变体。
顺便说一句,timsort是以其发明者tim peters命名的,在python循环中也被称为"tim bot"(作为python社区中的"bot",需要能够快速、正确地回答许多技术问题;tim是第一个如此受人尊敬的人)。第二个是F.Lundh,"Effbot"。后来我很荣幸地被命名为第三个(据我所知是最后一个),名为"马泰利机器人"。然而,我从来没有开发过比timsort酷十分之一的算法!-)
tl;dr:使用
另一个注意事项:
只对键进行排序,并使用
1 2 3 | def displayEmployees(self): for key in sorted(self.employees): print(key, self.employees[key], sep='\t') |
或直接对项目排序:
1 2 3 | def displayEmployees(self): for key, value in sorted(self.employees.items()): print(key, value, sep='\t') |
几天前,我用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | class EmployeeDict(dict): '''Just a dictionary, but with a better display of data. ''' def __str__(self): str_output ="" maxlen = len(str(max(self.keys()))) for key in sorted(self.keys()): str_output +="{} | {} ".format(str(key).rjust(maxlen), self[key]) return str_output class Company(object): def __init__(self, name): self.name = name self.employees = EmployeeDict() def addEmployee(self, id, name): self.employees[id] = name def displayEmployees(self): print(str(self.employees)) a = Company('The Company') a.addEmployee(111, 'Employee1') a.addEmployee(222, 'Employee2') a.addEmployee(333, 'Employee3') a.displayEmployees() |
输出:
1 2 3 | 111 | Employee1 222 | Employee2 333 | Employee3 |
普通听写不记得顺序。如果在代码的其余部分中保持dict的顺序对您很重要,那么另一种选择是使用Python的
1 2 3 4 5 6 7 | from collections import OrderedDict # ... def addEmployee(self, id, name): self.employees[id] = name self.employees = OrderedDict(sorted(self.employees.items())) |
这将始终保持您的
1 2 3 | def displayEmployees(self): for k,v in self.employees.items(): print(k, '\t', v) |