Ways of Sorting 2D lists in Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PythonB15
    New Member
    • May 2015
    • 7

    Ways of Sorting 2D lists in Python

    Hi all,
    I am hoping you can help me.

    I have a data structure which consists of a game players name and their scores, I have use lists within lists (2D):

    results = [["Ben", 15, 18,25, 6, 9], ["George", 25, 35, 85, 12, 9], ["Evie", 2, 54, 84, 62,18]]

    What I want to do is sort the list in alphabetical order with each players highest score.

    I know how to sort by specifying a specific index using Itemgetter, but unsure of how to do this by looking at all a players scores over the five attempts and then displaying the highest score.

    How would I go about doing this.....

    Thanks in advance
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Is this what you want to do?
    Code:
    >>> results = [["Ben", 15, 18,25, 6, 9], ["George", 25, 35, 85, 12, 9], ["Evie", 2, 54, 84, 62,18]]
    >>> for i, item in enumerate(results):
    ... 	results[i] = [item[0], max(item[1:])]
    ... 	
    >>> sorted(results)
    [['Ben', 25], ['Evie', 84], ['George', 85]]
    >>>

    Comment

    • PythonB15
      New Member
      • May 2015
      • 7

      #3
      Yes kind of, but I want to display all the players results, starting with their highest....

      Also can you explain briefly what the code does:

      for i, item in enumerate(resul ts):
      results[i] = [item[0], max(item[1:])]

      is "item in enumerate" a default python statement?
      the second line: results[i] = [item[0], max(item[1:])]
      can you please explain this to me?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        In your original post, you stated you wanted to display the highest score.

        enumerate is a built-in function. i and item are identifiers of the values returned by enumerate. From Python help:
        "enumerate(sequ ence[, start=0])
        Return an enumerate object. sequence must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the corresponding value obtained from iterating over iterable"
        Example:
        Code:
        >>> for i, item in enumerate(["January", "February", "March"]):
        ... 	print i, item
        ... 	
        0 January
        1 February
        2 March
        >>>
        Line 2 uses a the list index "0" to return the first item in the list and a list slice to return the items after the first item.

        There are numerous resources on the internet that will explain better that I can.

        Comment

        • PythonB15
          New Member
          • May 2015
          • 7

          #5
          Thank you very much, you have been brilliant.

          Comment

          • PythonB15
            New Member
            • May 2015
            • 7

            #6
            Does anybody have any other ways of doing this?

            Comment

            • computerfox
              Contributor
              • Mar 2010
              • 276

              #7
              You can also manually iterate through the list of lists, sorting the scores as you go, but the benefit of Python is the built in functions and loops that do that for you. What kind of solution are you looking for?

              Comment

              • PythonB15
                New Member
                • May 2015
                • 7

                #8
                Apologies if I ask the basic of questions as I am new to python.

                How would you manually iterate through a list of lists? I would like to know any other built in functions/loops that would do the same thing as BVDET has shown. I'm just curious and want to learn that's all.

                Comment

                Working...