第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 中查找所有數(shù)據(jù)列表和相關標題的最高值和最低值的最有效算法是什么

Python 中查找所有數(shù)據(jù)列表和相關標題的最高值和最低值的最有效算法是什么

蕪湖不蕪 2023-06-27 14:15:15
在我的程序中有多個測驗。用戶進行測驗,然后測驗的標題和分數(shù)將保存到數(shù)據(jù)庫中。為了便于示例,我將使用 Python 列表來表示它們:[['quizTitle1', score], ['quizTitle2',score] ['quizTitle1', score] ['quizTitle3', score]]我正在嘗試打印出用戶最弱的測驗標題。因此,使用 Python 列表示例,您會看到用戶已參加測驗 1兩次。在第二次測驗中,他們可能比第一次得到了更好的分數(shù)。因此,我需要獲得用戶在每次測驗中獲得的最高分(他們的最佳分數(shù))。然后我需要找出哪個測驗的分數(shù)最低、最好。我目前的計劃是這樣的(偽代碼)While found = false  1st = the first score selected that we are comparing with each other score  2nd = the score we are comparing to the first  For loop that repeats in the range of the number of lists    If (2nd < 1st) or (2nd has the same title and greater mark than 1st):       2nd becomes 1st       Loop repeats     Else:       New 2nd is the next list  Found = true   但最好的方法是什么?
查看完整描述

4 回答

?
慕婉清6462132

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 的mapreduce

在本例中,我們正在尋找最高分數(shù)中的最低分數(shù),因此在比較兩個元素時,我們希望保留兩者中的最小值。但在 python 中,我們可以直接調(diào)用整個序列,而不是min()重復應用該函數(shù)。reducemin()

僅供參考,如果我們使用的話,代碼將如下所示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ù),它按鍵分組,然后按鍵減少。這照顧了我們的groupbymax步驟;我們只需要減少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)


查看完整回答
反對 回復 2023-06-27
?
梵蒂岡之花

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


查看完整回答
反對 回復 2023-06-27
?
白板的微信

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é)果就是具有最小值的鍵。


查看完整回答
反對 回復 2023-06-27
?
鴻蒙傳說

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


查看完整回答
反對 回復 2023-06-27
  • 4 回答
  • 0 關注
  • 264 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號