t488.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. a = [2,1,-4,3,0,6]
  2. a.sort()
  3. print a
  4. b = "rksdubtheynjmpwqzlfiovxgac"
  5. print sorted(b,None,lambda x: ord(x))
  6. c = [2,1,-4,3,0,6]
  7. print sorted(c)
  8. print sorted(c, lambda x, y: y - x);
  9. class Test:
  10. def __init__(self, id, value):
  11. self.id = id
  12. self.value = value
  13. def __repr__(self):
  14. return "id: " + str(self.id) + " value: " + self.value
  15. d = [ Test(4, "test"), Test(3, "test"), Test(6, "test"), Test(1, "test"), Test(2, "test"), Test(9, "test"), Test(0, "test") ]
  16. print sorted(d, lambda x, y: y - x, lambda x: x.id, True)
  17. print c
  18. print sorted(c, None, None, True)
  19. c.sort(reverse=True)
  20. print c
  21. c.sort()
  22. print c
  23. c.sort(lambda x, y: y - x, lambda x: pow(x, 2), True)
  24. print c
  25. L = [7, 3, -2, 4]
  26. d = {'a': 5, 'b': 9}
  27. def g(k):
  28. return d[k]
  29. print(g('a'))
  30. print(sorted(d.keys(), None, g))
  31. print(sorted(d.keys(), None, None))
  32. print(sorted(d.keys(), None, lambda x: d[x]))
  33. def myabs(x):
  34. return abs(x)
  35. print(sorted(L, None, myabs))
  36. print(sorted(L, None, lambda x: myabs(x)))
  37. print(sorted(L, None, lambda x: abs(x)))
  38. print(sorted(L, None, abs))
  39. print(sorted(L, key=lambda x: -x, reverse=True))
  40. print(sorted(L, key=lambda x: -x))