Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MY_TUPLE = (
- ('A','Apple'),
- ('C','Carrot'),
- ('B','Banana'),
- )
- from opererator import itemgetter
- MY_SORTED_TUPLE = tuple(sorted(MY_TUPLE, key=itemgetter(1)))
- MY_SORTED_TUPLE = tuple(sorted(MY_TUPLE, key=lambda item: item[1]))
- >>> import operator
- >>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
- >>> map(operator.itemgetter(0), L)
- ['c', 'd', 'a', 'b']
- >>> map(operator.itemgetter(1), L)
- [2, 1, 4, 3]
- >>> sorted(L, key=operator.itemgetter(1))
- [('d', 1), ('c', 2), ('b', 3), ('a', 4)]
- sorted(my_tuple, key=lambda tup: tup[1])
- templist = [ (line[1], line) for line in MY_TUPLE ]
- templist.sort()
- SORTED_MY_TUPLE = [ line[1] for line in templist ]
Add Comment
Please, Sign In to add comment