Guest User

Untitled

a guest
Mar 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. >>> ut
  2. [<Tag: 128>, <Tag: 2008>, <Tag: <>, <Tag: actionscript>, <Tag: addresses>,
  3. <Tag: aes>, <Tag: ajax> ...]
  4.  
  5. >>> ut[1].count
  6. 1L
  7.  
  8. # To sort the list in place...
  9. ut.sort(key=lambda x: x.count, reverse=True)
  10.  
  11. # To return a new list, use the sorted() built-in function...
  12. newlist = sorted(ut, key=lambda x: x.count, reverse=True)
  13.  
  14. try: import operator
  15. except ImportError: keyfun= lambda x: x.count # use a lambda if no operator module
  16. else: keyfun= operator.attrgetter("count") # use operator since it's faster than lambda
  17.  
  18. ut.sort(key=keyfun, reverse=True) # sort in-place
  19.  
  20. ut.sort(key=lambda x: x.count, reverse=True)
  21.  
  22. #!/usr/bin/env python
  23. import random
  24.  
  25. class C:
  26. def __init__(self,count):
  27. self.count = count
  28.  
  29. def __cmp__(self,other):
  30. return cmp(self.count,other.count)
  31.  
  32. longList = [C(random.random()) for i in xrange(1000000)] #about 6.1 secs
  33. longList2 = longList[:]
  34.  
  35. longList.sort() #about 52 - 6.1 = 46 secs
  36. longList2.sort(key = lambda c: c.count) #about 9 - 6.1 = 3 secs
  37.  
  38. from operator import attrgetter
  39. ut.sort(key = attrgetter('count'), reverse = True)
  40.  
  41. ut = Tag.objects.order_by('-count')
  42.  
  43. class Card(object):
  44.  
  45. def __init__(self, rank, suit):
  46. self.rank = rank
  47. self.suit = suit
  48.  
  49. def __eq__(self, other):
  50. return self.rank == other.rank and self.suit == other.suit
  51.  
  52. def __lt__(self, other):
  53. return self.rank < other.rank
  54.  
  55. hand = [Card(10, 'H'), Card(2, 'h'), Card(12, 'h'), Card(13, 'h'), Card(14, 'h')]
  56. hand_order = [c.rank for c in hand] # [10, 2, 12, 13, 14]
  57.  
  58. hand_sorted = sorted(hand)
  59. hand_sorted_order = [c.rank for c in hand_sorted] # [2, 10, 12, 13, 14]
Add Comment
Please, Sign In to add comment