4 回答

TA貢獻1804條經(jīng)驗 獲得超2個贊
映射縮減方法:
from itertools import groupby
from operator import itemgetter
scores = [['q1', 20],['q2',30],['q1',40],['q2',10],['q2',45],['q1',10]]
name, score = itemgetter(0), itemgetter(1)
grouped_scores = groupby(sorted(scores), key=name)? ? ? ? ? ? ? # group by key
highest_scores = (max(g, key=score) for _,g in grouped_scores)? # reduce by key
lowest_highest = min(highest_scores, key=score)? ? ? ? ? ? ? ? ?# reduce
print(lowest_highest)
輸出:
['q1', 40]
解釋
和生成器表達式的返回值groupby
不是列表,如果您嘗試直接打印它們,您會看到一堆無用的<itertools._grouper object at 0x7ff18bbbb850>
.?但是使用將每個不可打印對象轉(zhuǎn)換為列表list()
,計算出的中間值如下:
scores = [['q1', 20],['q2',30],['q1',40],['q2',10],['q2',45],['q1',10]]
grouped_scores = [
? ['q1', [['q1', 10], ['q1', 20], ['q1', 40]]],
? ['q2', [['q2', 10], ['q2', 30], ['q2', 45]]]
]
highest_scores = [['q1', 40], ['q2', 45]]
lowest_highest = ['q1', 40]
Python 的map
和reduce
在本例中,我們正在尋找最高分數(shù)中的最低分數(shù),因此在比較兩個元素時,我們希望保留兩者中的最小值。但在 python 中,我們可以直接調(diào)用整個序列,而不是min()
重復應用該函數(shù)。reduce
min()
僅供參考,如果我們使用的話,代碼將如下所示reduce
:
from itertools import groupby
from functools import reduce
scores = [['q1', 20],['q2',30],['q1',40],['q2',10],['q2',45],['q1',10]]
name, score = itemgetter(0), itemgetter(1)
grouped_scores = groupby(sorted(scores), key=name)? # group by key
highest_scores = map(lambda x: max(x[1], key=score), grouped_scores)? # reduce by key
lowest_highest = reduce(lambda x,y: min(x,y, key=score), highest_scores)? # reduce
print(lowest_highest)
輸出:
['q1', 40]
使用模塊 more_itertools
模塊 more_itertools 有一個名為map_reduce的函數(shù),它按鍵分組,然后按鍵減少。這照顧了我們的groupby
和max
步驟;我們只需要減少min
就可以得到我們的結(jié)果。
from more_itertools import map_reduce
from operator import itemgetter
scores = [['q1', 20],['q2',30],['q1',40],['q2',10],['q2',45],['q1',10]]
name, score = itemgetter(0), itemgetter(1)
highest_scores = map_reduce(scores, keyfunc=name, valuefunc=score, reducefunc=max)
lowest_highest = min(highest_scores.items(), key=score)
print(lowest_highest)
# ('q1', 40)

TA貢獻1900條經(jīng)驗 獲得超5個贊
好吧,如果您愿意pandas
,那么:
import pandas as pd
l = [["quizTitle1", 15],
? ? ?["quizTitle2", 25],
? ? ?["quizTitle1", 11],
? ? ?["quizTitle3", 84],
? ? ?["quizTitle2", 24]]
df = pd.DataFrame(l, columns=["quiz", "score"])
print(df)
#? ? ? ? ? quiz? score
# 0? quizTitle1? ? ?15
# 1? quizTitle2? ? ?25
# 2? quizTitle1? ? ?11
# 3? quizTitle3? ? ?84
# 4? quizTitle2? ? ?24
lowest_score = df.iloc[df.groupby(['quiz']).max().reset_index()["score"].idxmin()]
print(lowest_score)
# quiz? ? ?quizTitle1
# score? ? ? ? ? ? 15
# Name: 0, dtype: object

TA貢獻1883條經(jīng)驗 獲得超3個贊
一個簡單的:
scores = [['q1', 20],['q2',30],['q1',40],['q2',10],['q2',45],['q1',10]]
d = dict(sorted(scores))
print(min(d, key=d.get)) # prints q1
該dict函數(shù)采用鍵/值對,我們只需要首先對它們進行排序,以便每個鍵的最后一個值是它最大的(因為最后一個是最終出現(xiàn)在字典中的值)。之后,所需的結(jié)果就是具有最小值的鍵。

TA貢獻1865條經(jīng)驗 獲得超7個贊
您可以使用字典來存儲每個測驗的值,并用列表中迄今為止看到的最大值更新其值,然后獲取字典中所有值的最小值。
scores = [['q1', 20],['q2',30],['q1',40],['q2',10],['q2',45],['q1',10]]
d = {}
for s in scores:
d[s[0]] = s[1] if s[0] not in d else max(d[s[0]], s[1])
print(d)
print("Lowest best : ", min(d.values()))
這打?。?/p>
{'q1': 40, 'q2': 45}
Lowest best : 40
添加回答
舉報