Advertisement
Guest User

Untitled

a guest
Feb 4th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. class Thing:
  2. def __init__(self, value):
  3. self.value = value
  4. def __lt__(self, other):
  5. return self.value < other.value
  6. def __eq__(self, other):
  7. return self.value == other.value
  8. def __repr__(self):
  9. return f"Thing({repr(self.value)})"
  10. def __hash__(self):
  11. return super().__hash__()
  12.  
  13. def kevin_sorted(d):
  14. return {k: d[k] for k in sorted(d.keys())}
  15.  
  16. def andras_sorted(d):
  17. return dict(sorted(d.items()))
  18.  
  19. a = Thing(1)
  20. b = Thing(1)
  21. c = Thing(2)
  22.  
  23. d = {c: "X", a: "Z", b: "Y"}
  24. print(kevin_sorted(d))
  25. #{Thing(1): 'Z', Thing(1): 'Y', Thing(2): 'X'}
  26. print(andras_sorted(d))
  27. #{Thing(1): 'Y', Thing(1): 'Z', Thing(2): 'X'}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement