Make a new list depending on group number and add scores up as well










6















If a have a list within a another list that looks like this...



[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]


How can I add the middle element together so so for 'Harry' for example, it shows up as ['Harry', 26] and also for Python to look at the group number (3rd element) and output the winner only (the one with the highest score which is the middle element). So for each group, there needs to be one winner. So the final output shows:



[['Harry', 26],['Sam',21]]


THIS QUESTION IS NOT A DUPLICATE: It has a third element as well which I am stuck about



The similar question gave me an answer of:



grouped_scores = 
for name, score, group_number in players_info:
if name not in grouped_scores:
grouped_scores[name] = score
grouped_scores[group_number] = group_number
else:
grouped_scores[name] += score


But that only adds the scores up, it doesn't take out the winner from each group. Please help.



I had thought doing something like this, but I'm not sure exactly what to do...



grouped_scores = 
for name, score, group_number in players_info:
if name not in grouped_scores:
grouped_scores[name] = score
else:
grouped_scores[name] += score
for group in group_number:
if grouped_scores[group_number] = group_number:
[don't know what to do here]









share|improve this question




























    6















    If a have a list within a another list that looks like this...



    [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]


    How can I add the middle element together so so for 'Harry' for example, it shows up as ['Harry', 26] and also for Python to look at the group number (3rd element) and output the winner only (the one with the highest score which is the middle element). So for each group, there needs to be one winner. So the final output shows:



    [['Harry', 26],['Sam',21]]


    THIS QUESTION IS NOT A DUPLICATE: It has a third element as well which I am stuck about



    The similar question gave me an answer of:



    grouped_scores = 
    for name, score, group_number in players_info:
    if name not in grouped_scores:
    grouped_scores[name] = score
    grouped_scores[group_number] = group_number
    else:
    grouped_scores[name] += score


    But that only adds the scores up, it doesn't take out the winner from each group. Please help.



    I had thought doing something like this, but I'm not sure exactly what to do...



    grouped_scores = 
    for name, score, group_number in players_info:
    if name not in grouped_scores:
    grouped_scores[name] = score
    else:
    grouped_scores[name] += score
    for group in group_number:
    if grouped_scores[group_number] = group_number:
    [don't know what to do here]









    share|improve this question


























      6












      6








      6


      0






      If a have a list within a another list that looks like this...



      [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]


      How can I add the middle element together so so for 'Harry' for example, it shows up as ['Harry', 26] and also for Python to look at the group number (3rd element) and output the winner only (the one with the highest score which is the middle element). So for each group, there needs to be one winner. So the final output shows:



      [['Harry', 26],['Sam',21]]


      THIS QUESTION IS NOT A DUPLICATE: It has a third element as well which I am stuck about



      The similar question gave me an answer of:



      grouped_scores = 
      for name, score, group_number in players_info:
      if name not in grouped_scores:
      grouped_scores[name] = score
      grouped_scores[group_number] = group_number
      else:
      grouped_scores[name] += score


      But that only adds the scores up, it doesn't take out the winner from each group. Please help.



      I had thought doing something like this, but I'm not sure exactly what to do...



      grouped_scores = 
      for name, score, group_number in players_info:
      if name not in grouped_scores:
      grouped_scores[name] = score
      else:
      grouped_scores[name] += score
      for group in group_number:
      if grouped_scores[group_number] = group_number:
      [don't know what to do here]









      share|improve this question
















      If a have a list within a another list that looks like this...



      [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]


      How can I add the middle element together so so for 'Harry' for example, it shows up as ['Harry', 26] and also for Python to look at the group number (3rd element) and output the winner only (the one with the highest score which is the middle element). So for each group, there needs to be one winner. So the final output shows:



      [['Harry', 26],['Sam',21]]


      THIS QUESTION IS NOT A DUPLICATE: It has a third element as well which I am stuck about



      The similar question gave me an answer of:



      grouped_scores = 
      for name, score, group_number in players_info:
      if name not in grouped_scores:
      grouped_scores[name] = score
      grouped_scores[group_number] = group_number
      else:
      grouped_scores[name] += score


      But that only adds the scores up, it doesn't take out the winner from each group. Please help.



      I had thought doing something like this, but I'm not sure exactly what to do...



      grouped_scores = 
      for name, score, group_number in players_info:
      if name not in grouped_scores:
      grouped_scores[name] = score
      else:
      grouped_scores[name] += score
      for group in group_number:
      if grouped_scores[group_number] = group_number:
      [don't know what to do here]






      python arrays python-3.x list






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 10:04









      U9-Forward

      16k51543




      16k51543










      asked Nov 14 '18 at 8:03







      user10650570





























          4 Answers
          4






          active

          oldest

          votes


















          6














          Use itertools.groupby, and collections.defaultdict:



          l=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
          from itertools import groupby
          from collections import defaultdict
          l2=[list(y) for x,y in groupby(l,key=lambda x: x[-1])]
          l3=
          for x in l2:
          d=defaultdict(int)
          for x,y,z in x:
          d[x]+=y
          l3.append(max(list(map(list,dict(d).items())),key=lambda x: x[-1]))


          Now:



          print(l3)


          Is:



          [['Harry', 26], ['Sam', 21]]





          share|improve this answer


















          • 1





            Can you please just add in comments so I can learn and understand what each line is doing?

            – user10650570
            Nov 14 '18 at 9:29







          • 1





            @Harry First two lines are importing modules, then next line is using groupby to separate in to two groups based on last element of each sub-list, next line to create empty list, next loop iterating trough the grouped ones, then create a defaultdict, then the sub-loop is adding the stuff to the defaultdict, then last line to manage how to make that dictionary into a list.

            – U9-Forward
            Nov 14 '18 at 9:39











          • @Harry Happy to help, :-), 😊😊😊😊

            – U9-Forward
            Nov 14 '18 at 9:39






          • 1





            Thank you more the help!!

            – user10650570
            Nov 14 '18 at 9:47











          • @Harry YW. again. :D

            – U9-Forward
            Nov 14 '18 at 10:05


















          0














          you can try to use Counter and it's method most_common




          Return a list of the n most common elements and their counts from the
          most common to the least




          from collections import Counter
          l = [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
          step = 3
          results = list()
          for x in range(0, len(l), step):
          cnt = Counter()
          for y in l[x: x+step]:
          cnt.update(y[0]: y[1])
          results.append(cnt.most_common(1)[0])


          will give you:



          print(results)
          [('Harry', 26), ('Sam', 21)]





          share|improve this answer
































            0














            I would aggregate the data first with a defaultdict.



            >>> from collections import defaultdict
            >>>
            >>> combined = defaultdict(lambda: defaultdict(int))
            >>> data = [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
            >>>
            >>> for name, score, group in data:
            ...: combined[group][name] += score
            ...:
            >>> combined
            >>>
            defaultdict(<function __main__.<lambda>()>,
            1: defaultdict(int, 'Harry': 26, 'Jake': 4),
            2: defaultdict(int, 'Dave': 9, 'Sam': 21))


            Then apply max to each value in that dict.



            >>> from operator import itemgetter
            >>> [list(max(v.items(), key=itemgetter(1))) for v in combined.values()]
            >>> [['Harry', 26], ['Sam', 21]]





            share|improve this answer






























              0














              use itertools.groupby and then take the middle value from the grouped element and then append it to a list passed on the maximum condition



              import itertools
              l=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
              maxlist=
              maxmiddleindexvalue=0
              for key,value in itertools.groupby(l,key=lambda x:x[0]):
              s=0
              m=0
              for element in value:
              s+=element[1]
              m=max(m,element[1])
              if(m==maxmiddleindexvalue):
              maxlist.append([(key,s)])

              if(m>maxmiddleindexvalue):
              maxlist=[(key,s)]
              maxmiddleindexvalue=m

              print(maxlist)


              OUTPUT



              [('Harry', 26), [('Sam', 21)]]





              share|improve this answer






















                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295489%2fmake-a-new-list-depending-on-group-number-and-add-scores-up-as-well%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown
























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                6














                Use itertools.groupby, and collections.defaultdict:



                l=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                from itertools import groupby
                from collections import defaultdict
                l2=[list(y) for x,y in groupby(l,key=lambda x: x[-1])]
                l3=
                for x in l2:
                d=defaultdict(int)
                for x,y,z in x:
                d[x]+=y
                l3.append(max(list(map(list,dict(d).items())),key=lambda x: x[-1]))


                Now:



                print(l3)


                Is:



                [['Harry', 26], ['Sam', 21]]





                share|improve this answer


















                • 1





                  Can you please just add in comments so I can learn and understand what each line is doing?

                  – user10650570
                  Nov 14 '18 at 9:29







                • 1





                  @Harry First two lines are importing modules, then next line is using groupby to separate in to two groups based on last element of each sub-list, next line to create empty list, next loop iterating trough the grouped ones, then create a defaultdict, then the sub-loop is adding the stuff to the defaultdict, then last line to manage how to make that dictionary into a list.

                  – U9-Forward
                  Nov 14 '18 at 9:39











                • @Harry Happy to help, :-), 😊😊😊😊

                  – U9-Forward
                  Nov 14 '18 at 9:39






                • 1





                  Thank you more the help!!

                  – user10650570
                  Nov 14 '18 at 9:47











                • @Harry YW. again. :D

                  – U9-Forward
                  Nov 14 '18 at 10:05















                6














                Use itertools.groupby, and collections.defaultdict:



                l=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                from itertools import groupby
                from collections import defaultdict
                l2=[list(y) for x,y in groupby(l,key=lambda x: x[-1])]
                l3=
                for x in l2:
                d=defaultdict(int)
                for x,y,z in x:
                d[x]+=y
                l3.append(max(list(map(list,dict(d).items())),key=lambda x: x[-1]))


                Now:



                print(l3)


                Is:



                [['Harry', 26], ['Sam', 21]]





                share|improve this answer


















                • 1





                  Can you please just add in comments so I can learn and understand what each line is doing?

                  – user10650570
                  Nov 14 '18 at 9:29







                • 1





                  @Harry First two lines are importing modules, then next line is using groupby to separate in to two groups based on last element of each sub-list, next line to create empty list, next loop iterating trough the grouped ones, then create a defaultdict, then the sub-loop is adding the stuff to the defaultdict, then last line to manage how to make that dictionary into a list.

                  – U9-Forward
                  Nov 14 '18 at 9:39











                • @Harry Happy to help, :-), 😊😊😊😊

                  – U9-Forward
                  Nov 14 '18 at 9:39






                • 1





                  Thank you more the help!!

                  – user10650570
                  Nov 14 '18 at 9:47











                • @Harry YW. again. :D

                  – U9-Forward
                  Nov 14 '18 at 10:05













                6












                6








                6







                Use itertools.groupby, and collections.defaultdict:



                l=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                from itertools import groupby
                from collections import defaultdict
                l2=[list(y) for x,y in groupby(l,key=lambda x: x[-1])]
                l3=
                for x in l2:
                d=defaultdict(int)
                for x,y,z in x:
                d[x]+=y
                l3.append(max(list(map(list,dict(d).items())),key=lambda x: x[-1]))


                Now:



                print(l3)


                Is:



                [['Harry', 26], ['Sam', 21]]





                share|improve this answer













                Use itertools.groupby, and collections.defaultdict:



                l=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                from itertools import groupby
                from collections import defaultdict
                l2=[list(y) for x,y in groupby(l,key=lambda x: x[-1])]
                l3=
                for x in l2:
                d=defaultdict(int)
                for x,y,z in x:
                d[x]+=y
                l3.append(max(list(map(list,dict(d).items())),key=lambda x: x[-1]))


                Now:



                print(l3)


                Is:



                [['Harry', 26], ['Sam', 21]]






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 8:18









                U9-ForwardU9-Forward

                16k51543




                16k51543







                • 1





                  Can you please just add in comments so I can learn and understand what each line is doing?

                  – user10650570
                  Nov 14 '18 at 9:29







                • 1





                  @Harry First two lines are importing modules, then next line is using groupby to separate in to two groups based on last element of each sub-list, next line to create empty list, next loop iterating trough the grouped ones, then create a defaultdict, then the sub-loop is adding the stuff to the defaultdict, then last line to manage how to make that dictionary into a list.

                  – U9-Forward
                  Nov 14 '18 at 9:39











                • @Harry Happy to help, :-), 😊😊😊😊

                  – U9-Forward
                  Nov 14 '18 at 9:39






                • 1





                  Thank you more the help!!

                  – user10650570
                  Nov 14 '18 at 9:47











                • @Harry YW. again. :D

                  – U9-Forward
                  Nov 14 '18 at 10:05












                • 1





                  Can you please just add in comments so I can learn and understand what each line is doing?

                  – user10650570
                  Nov 14 '18 at 9:29







                • 1





                  @Harry First two lines are importing modules, then next line is using groupby to separate in to two groups based on last element of each sub-list, next line to create empty list, next loop iterating trough the grouped ones, then create a defaultdict, then the sub-loop is adding the stuff to the defaultdict, then last line to manage how to make that dictionary into a list.

                  – U9-Forward
                  Nov 14 '18 at 9:39











                • @Harry Happy to help, :-), 😊😊😊😊

                  – U9-Forward
                  Nov 14 '18 at 9:39






                • 1





                  Thank you more the help!!

                  – user10650570
                  Nov 14 '18 at 9:47











                • @Harry YW. again. :D

                  – U9-Forward
                  Nov 14 '18 at 10:05







                1




                1





                Can you please just add in comments so I can learn and understand what each line is doing?

                – user10650570
                Nov 14 '18 at 9:29






                Can you please just add in comments so I can learn and understand what each line is doing?

                – user10650570
                Nov 14 '18 at 9:29





                1




                1





                @Harry First two lines are importing modules, then next line is using groupby to separate in to two groups based on last element of each sub-list, next line to create empty list, next loop iterating trough the grouped ones, then create a defaultdict, then the sub-loop is adding the stuff to the defaultdict, then last line to manage how to make that dictionary into a list.

                – U9-Forward
                Nov 14 '18 at 9:39





                @Harry First two lines are importing modules, then next line is using groupby to separate in to two groups based on last element of each sub-list, next line to create empty list, next loop iterating trough the grouped ones, then create a defaultdict, then the sub-loop is adding the stuff to the defaultdict, then last line to manage how to make that dictionary into a list.

                – U9-Forward
                Nov 14 '18 at 9:39













                @Harry Happy to help, :-), 😊😊😊😊

                – U9-Forward
                Nov 14 '18 at 9:39





                @Harry Happy to help, :-), 😊😊😊😊

                – U9-Forward
                Nov 14 '18 at 9:39




                1




                1





                Thank you more the help!!

                – user10650570
                Nov 14 '18 at 9:47





                Thank you more the help!!

                – user10650570
                Nov 14 '18 at 9:47













                @Harry YW. again. :D

                – U9-Forward
                Nov 14 '18 at 10:05





                @Harry YW. again. :D

                – U9-Forward
                Nov 14 '18 at 10:05













                0














                you can try to use Counter and it's method most_common




                Return a list of the n most common elements and their counts from the
                most common to the least




                from collections import Counter
                l = [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                step = 3
                results = list()
                for x in range(0, len(l), step):
                cnt = Counter()
                for y in l[x: x+step]:
                cnt.update(y[0]: y[1])
                results.append(cnt.most_common(1)[0])


                will give you:



                print(results)
                [('Harry', 26), ('Sam', 21)]





                share|improve this answer





























                  0














                  you can try to use Counter and it's method most_common




                  Return a list of the n most common elements and their counts from the
                  most common to the least




                  from collections import Counter
                  l = [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                  step = 3
                  results = list()
                  for x in range(0, len(l), step):
                  cnt = Counter()
                  for y in l[x: x+step]:
                  cnt.update(y[0]: y[1])
                  results.append(cnt.most_common(1)[0])


                  will give you:



                  print(results)
                  [('Harry', 26), ('Sam', 21)]





                  share|improve this answer



























                    0












                    0








                    0







                    you can try to use Counter and it's method most_common




                    Return a list of the n most common elements and their counts from the
                    most common to the least




                    from collections import Counter
                    l = [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                    step = 3
                    results = list()
                    for x in range(0, len(l), step):
                    cnt = Counter()
                    for y in l[x: x+step]:
                    cnt.update(y[0]: y[1])
                    results.append(cnt.most_common(1)[0])


                    will give you:



                    print(results)
                    [('Harry', 26), ('Sam', 21)]





                    share|improve this answer















                    you can try to use Counter and it's method most_common




                    Return a list of the n most common elements and their counts from the
                    most common to the least




                    from collections import Counter
                    l = [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                    step = 3
                    results = list()
                    for x in range(0, len(l), step):
                    cnt = Counter()
                    for y in l[x: x+step]:
                    cnt.update(y[0]: y[1])
                    results.append(cnt.most_common(1)[0])


                    will give you:



                    print(results)
                    [('Harry', 26), ('Sam', 21)]






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 14 '18 at 8:24

























                    answered Nov 14 '18 at 8:18









                    Bear BrownBear Brown

                    12.1k82245




                    12.1k82245





















                        0














                        I would aggregate the data first with a defaultdict.



                        >>> from collections import defaultdict
                        >>>
                        >>> combined = defaultdict(lambda: defaultdict(int))
                        >>> data = [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                        >>>
                        >>> for name, score, group in data:
                        ...: combined[group][name] += score
                        ...:
                        >>> combined
                        >>>
                        defaultdict(<function __main__.<lambda>()>,
                        1: defaultdict(int, 'Harry': 26, 'Jake': 4),
                        2: defaultdict(int, 'Dave': 9, 'Sam': 21))


                        Then apply max to each value in that dict.



                        >>> from operator import itemgetter
                        >>> [list(max(v.items(), key=itemgetter(1))) for v in combined.values()]
                        >>> [['Harry', 26], ['Sam', 21]]





                        share|improve this answer



























                          0














                          I would aggregate the data first with a defaultdict.



                          >>> from collections import defaultdict
                          >>>
                          >>> combined = defaultdict(lambda: defaultdict(int))
                          >>> data = [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                          >>>
                          >>> for name, score, group in data:
                          ...: combined[group][name] += score
                          ...:
                          >>> combined
                          >>>
                          defaultdict(<function __main__.<lambda>()>,
                          1: defaultdict(int, 'Harry': 26, 'Jake': 4),
                          2: defaultdict(int, 'Dave': 9, 'Sam': 21))


                          Then apply max to each value in that dict.



                          >>> from operator import itemgetter
                          >>> [list(max(v.items(), key=itemgetter(1))) for v in combined.values()]
                          >>> [['Harry', 26], ['Sam', 21]]





                          share|improve this answer

























                            0












                            0








                            0







                            I would aggregate the data first with a defaultdict.



                            >>> from collections import defaultdict
                            >>>
                            >>> combined = defaultdict(lambda: defaultdict(int))
                            >>> data = [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                            >>>
                            >>> for name, score, group in data:
                            ...: combined[group][name] += score
                            ...:
                            >>> combined
                            >>>
                            defaultdict(<function __main__.<lambda>()>,
                            1: defaultdict(int, 'Harry': 26, 'Jake': 4),
                            2: defaultdict(int, 'Dave': 9, 'Sam': 21))


                            Then apply max to each value in that dict.



                            >>> from operator import itemgetter
                            >>> [list(max(v.items(), key=itemgetter(1))) for v in combined.values()]
                            >>> [['Harry', 26], ['Sam', 21]]





                            share|improve this answer













                            I would aggregate the data first with a defaultdict.



                            >>> from collections import defaultdict
                            >>>
                            >>> combined = defaultdict(lambda: defaultdict(int))
                            >>> data = [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                            >>>
                            >>> for name, score, group in data:
                            ...: combined[group][name] += score
                            ...:
                            >>> combined
                            >>>
                            defaultdict(<function __main__.<lambda>()>,
                            1: defaultdict(int, 'Harry': 26, 'Jake': 4),
                            2: defaultdict(int, 'Dave': 9, 'Sam': 21))


                            Then apply max to each value in that dict.



                            >>> from operator import itemgetter
                            >>> [list(max(v.items(), key=itemgetter(1))) for v in combined.values()]
                            >>> [['Harry', 26], ['Sam', 21]]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 14 '18 at 8:24









                            timgebtimgeb

                            51.1k116693




                            51.1k116693





















                                0














                                use itertools.groupby and then take the middle value from the grouped element and then append it to a list passed on the maximum condition



                                import itertools
                                l=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                                maxlist=
                                maxmiddleindexvalue=0
                                for key,value in itertools.groupby(l,key=lambda x:x[0]):
                                s=0
                                m=0
                                for element in value:
                                s+=element[1]
                                m=max(m,element[1])
                                if(m==maxmiddleindexvalue):
                                maxlist.append([(key,s)])

                                if(m>maxmiddleindexvalue):
                                maxlist=[(key,s)]
                                maxmiddleindexvalue=m

                                print(maxlist)


                                OUTPUT



                                [('Harry', 26), [('Sam', 21)]]





                                share|improve this answer



























                                  0














                                  use itertools.groupby and then take the middle value from the grouped element and then append it to a list passed on the maximum condition



                                  import itertools
                                  l=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                                  maxlist=
                                  maxmiddleindexvalue=0
                                  for key,value in itertools.groupby(l,key=lambda x:x[0]):
                                  s=0
                                  m=0
                                  for element in value:
                                  s+=element[1]
                                  m=max(m,element[1])
                                  if(m==maxmiddleindexvalue):
                                  maxlist.append([(key,s)])

                                  if(m>maxmiddleindexvalue):
                                  maxlist=[(key,s)]
                                  maxmiddleindexvalue=m

                                  print(maxlist)


                                  OUTPUT



                                  [('Harry', 26), [('Sam', 21)]]





                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    use itertools.groupby and then take the middle value from the grouped element and then append it to a list passed on the maximum condition



                                    import itertools
                                    l=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                                    maxlist=
                                    maxmiddleindexvalue=0
                                    for key,value in itertools.groupby(l,key=lambda x:x[0]):
                                    s=0
                                    m=0
                                    for element in value:
                                    s+=element[1]
                                    m=max(m,element[1])
                                    if(m==maxmiddleindexvalue):
                                    maxlist.append([(key,s)])

                                    if(m>maxmiddleindexvalue):
                                    maxlist=[(key,s)]
                                    maxmiddleindexvalue=m

                                    print(maxlist)


                                    OUTPUT



                                    [('Harry', 26), [('Sam', 21)]]





                                    share|improve this answer













                                    use itertools.groupby and then take the middle value from the grouped element and then append it to a list passed on the maximum condition



                                    import itertools
                                    l=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]
                                    maxlist=
                                    maxmiddleindexvalue=0
                                    for key,value in itertools.groupby(l,key=lambda x:x[0]):
                                    s=0
                                    m=0
                                    for element in value:
                                    s+=element[1]
                                    m=max(m,element[1])
                                    if(m==maxmiddleindexvalue):
                                    maxlist.append([(key,s)])

                                    if(m>maxmiddleindexvalue):
                                    maxlist=[(key,s)]
                                    maxmiddleindexvalue=m

                                    print(maxlist)


                                    OUTPUT



                                    [('Harry', 26), [('Sam', 21)]]






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 14 '18 at 8:29









                                    Albin PaulAlbin Paul

                                    1,546718




                                    1,546718



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295489%2fmake-a-new-list-depending-on-group-number-and-add-scores-up-as-well%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Kleinkühnau

                                        Makov (Slowakei)

                                        Deutsches Schauspielhaus