Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # sort_by_many_attributes_via_lambda_demo.py
- sss = [
- ('tmp', 'bmp', 1000, 1200, 1200),
- ('xyz', 'jpg', 600, 1200, 720),
- ('123', 'jpg', 600, 1000, 600),
- ('foo', 'jpg', 800, 1000, 800),
- ('25', 'jpg', 800, 800, 640),
- ('bar', 'gif', 1000, 800, 800),
- ('abc', 'jpg', 600, 800, 480),
- ('456', 'gif', 800, 1200, 960),
- ('s25', 'gif', 1000, 1000, 1000)]
- options = '1:Filename 2:Type 3:Width 4:Height 5:Size'.split()
- def keyattr():
- for s in sss: print '\t\t\t%s\t%s\t%s\t%s\t%s'%(s)
- return raw_input("\nSort List By... \
- \n"+' '.join(options)+" (Q:To Exit) \
- \nAnd Re-Enter Attribute # To Reverse The Order. \
- \nKey Selection ::: ")
- choice = keyattr()
- while choice.lower() != 'q':
- if choice in '12345':
- i = int(choice)-1
- tmp=sss
- sss=sorted(sss,key=lambda x: x[i]) # <<<<<
- for s in range(len(sss)):
- if sss[s][i] != tmp[s][i]: tmp=1; break
- if tmp != 1: sss=sss[::-1] # sss=reversed(sss, reverse=True) as also sss=reversed(sss) does not work properly
- print;print
- print 'Option -- '+options[i]+'\n'
- choice = keyattr()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement