根据组数制作一个新列表并添加分数

如果在另一个列表中有一个列表,看起来像这样......


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

我如何将中间元素加在一起,例如,对于“Harry”,它会显示['Harry', 26]为 Python 查看组号(第 3 个元素)并仅输出获胜者(得分最高的那个是中间元素)。因此,对于每个组,需要有一个获胜者。所以最终输出显示:


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

这个问题不是重复的:它还有第三个元素,我对此很困惑


类似的问题给了我一个答案:


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

但这只是将分数相加,并不会从每组中取出获胜者。请帮忙。


我曾想过做这样的事情,但我不确定该怎么做......


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]


12345678_0001
浏览 192回答 3
3回答

陪伴而非守候

解决方案:使用itertools.groupby, 和collections.defaultdict:l=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]from itertools import groupbyfrom collections import defaultdictl2=[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]))现在:print(l3)是:[['Harry', 26], ['Sam', 21]]解释:前两行是导入模块。然后下一行groupby用于根据每个子列表的最后一个元素分为两组。然后下一行创建空列表。然后下一个循环遍历分组的循环。然后创建一个defaultdict. 然后子循环将内容添加到defaultdict. 然后最后一行来管理如何将该字典变成一个列表。

ITMISS

我会先用defaultdict.>>> from collections import defaultdict>>>&nbsp;>>> combined = defaultdict(lambda: defaultdict(int))>>> data = [['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]>>>&nbsp;>>> for name, score, group in data:...:&nbsp; &nbsp; combined[group][name] += score...:&nbsp; &nbsp;&nbsp;>>> combined>>>&nbsp;defaultdict(<function __main__.<lambda>()>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {1: defaultdict(int, {'Harry': 26, 'Jake': 4}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2: defaultdict(int, {'Dave': 9, 'Sam': 21})})然后应用于max该字典中的每个值。>>> from operator import itemgetter>>> [list(max(v.items(), key=itemgetter(1))) for v in combined.values()]>>> [['Harry', 26], ['Sam', 21]]

慕尼黑的夜晚无繁华

使用itertools.groupby然后从分组元素中取出中间值,然后将其附加到在最大条件下传递的列表中import itertoolsl=[['Harry',9,1],['Harry',17,1],['Jake',4,1], ['Dave',9,2],['Sam',17,2],['Sam',4,2]]maxlist=[]maxmiddleindexvalue=0for key,value in itertools.groupby(l,key=lambda x:x[0]):&nbsp; &nbsp; s=0&nbsp; &nbsp; m=0&nbsp; &nbsp; for element in value:&nbsp; &nbsp; &nbsp; &nbsp; s+=element[1]&nbsp; &nbsp; &nbsp; &nbsp; m=max(m,element[1])&nbsp; &nbsp; if(m==maxmiddleindexvalue):&nbsp; &nbsp; &nbsp; &nbsp; maxlist.append([(key,s)])&nbsp; &nbsp; if(m>maxmiddleindexvalue):&nbsp; &nbsp; &nbsp; &nbsp; maxlist=[(key,s)]&nbsp; &nbsp; &nbsp; &nbsp; maxmiddleindexvalue=mprint(maxlist)输出[('Harry', 26), [('Sam', 21)]]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python