Sort Tuples Python

后端 未结 3 1756
一向
一向 2020-12-31 01:23

I have a list of tuples in my Blender python code

scores=[(1489,\"Sean\"), (2850,\"Bob\"), (276,\"Crap Player\"), (78495, \"Great Player\"), (8473, \"Damian\         


        
相关标签:
3条回答
  • 2020-12-31 01:55

    sorted() returns the sorted sequence. If you want to sort a list in place then use list.sort().

    0 讨论(0)
  • 2020-12-31 01:59
    scores.sort(reverse=True)
    >>> print scores
    [(78495, 'Great Player'), (8473, 'Damian'), (4860, 'Andy'), (2850, 'Bob'), (1489, 'Sean'), (276, 'Crap Player'), (0, 'Stephen')]
    
    0 讨论(0)
  • 2020-12-31 02:16

    Just do:

    print sorted(scores, reverse=True)
    [(78495, 'Great Player'), (8473, 'Damian'), (4860, 'Andy'), (2850, 'Bob'), (1489, 'Sean'), (276, 'Crap Player'), (0, 'Stephen')]
    

    you can use scores.sort(reverse=True) if you want to sort in place, and by the way the sort function in case of list of tuple by default sort by first item , second item ..

    0 讨论(0)
提交回复
热议问题