2017-06-12 99 views
-1

我有這個腳本來獲取最高值+它的名稱的變量。如果有更多變量具有相同的最高變量,那麼它將返回所有這些變量。如果你不明白我在說什麼,這就是我的意思是:Check if something in a dictionary is the same as the max value in that dictionary?查找字符串是否在變量

腳本

if request.method == "POST": 
     d = {'g_dirt4': g_dirt4, 'g_destiny2': g_destiny2, 'g_southpark': g_southpark, 'g_codww2': g_codww2, 'g_bfront2': g_bfront2, 'g_reddead2': g_reddead2, 'g_fifa18': g_fifa18, 'g_motogp17': g_motogp17, 'g_elderscrolls': g_elderscrolls, 'g_crashbandicoot': g_crashbandicoot} 
     print("g_dirt4", g_dirt4, "g_destiny2", g_destiny2, "g_southpark", g_southpark, "g_codww2", g_codww2, "g_bfront2", g_bfront2, "g_reddead2", g_reddead2, "g_fifa18", g_fifa18, "g_motogp17", g_motogp17, "g_elderscrolls", g_elderscrolls, "g_crashbandicoot", g_crashbandicoot) 
     #print (max(d.items(), key=lambda x: x[1])) 
     result = [(n,v) for n,v in d.items() if v == max(d.values())] 
     print ("Resultaat", result) 
     if 'g_dirt4' in result: 
      print ('Dirt 4') 
     if 'g_destiny2' in result: 
      print ('Destiny 2') 
     if 'g_southpark' in result: 
      print ('South Park: The Fractured but Whole') 
     if 'g_codww2' in result: 
      print ('Call of Duty: WWII') 
     if 'g_bfront2' in result: 
      print ('Star Wars Battlefront II') 
     if 'g_reddead2' in result: 
      print ('Red Dead Redemption 2') 
     if 'g_fifa18' in result: 
      print ('FIFA 18') 
     if 'g_motogp17' in result: 
      print ('MotoGP™17') 
     if 'g_elderscrolls' in result: 
      print ('The Elder Scrolls Online: Morrowind') 
     if 'g_crashbandicoot' in result: 
      print ('Crash Bandicoot N. Sane Trilogy') 
     return redirect("https://i.vimeocdn.com/portrait/8487168_300x300") 

的問題是,它只返回(「Resultaat」,結果)和(「g_dirt4 」 g_dirt4, 「g_destiny2」 ..........),它看起來像這樣的例子:

2017-06-12 11:29:10 g_dirt4 8 g_destiny2 5 g_southpark 4 g_codww2 5 g_bfront2 6 g_reddead2 5 g_fifa18 6 g_motogp17 8 g_elderscrolls 7 g_crashbandicoot 5 
2017-06-12 11:29:10 Resultaat [('g_dirt4', 8), ('g_motogp17', 8)] 

但它不會返回這些:

if 'g_dirt4' in result: 
     print ('Dirt 4') 
...... 

我想檢查結果中的哪個遊戲。我該怎麼做呢?

編輯:

完整劇本https://pastebin.com/eKVnjJka

+2

您正在將'result'構建爲成對列表。然後你檢查各種字符串是否是列表的元素(即等於這些對中的任何一個)。字符串不等於對,所以你的'if'不匹配。也許你想建立一個鍵列表而不是一個列表對。 – khelwood

+0

這就像我說的:Resultaat [('g_dirt4',8),('g_motogp17',8)] – Jip1912

+0

這些變量在哪裏聲明? 'g_dirt4,...' –

回答

1

這裏是你如何可能發現組映射到在你的字典最大值鍵。

d = {'g_dirt4': g_dirt4, 'g_destiny2': g_destiny2, ... } 

max_value = max(d.values()) 
maximal_keys = { k for k,v in d.items() if v==max_value } 

if 'g_dirt4' in maximal_keys: 
    etc. 

我用一組爲maximal_keys,因爲它是查找更加高效,但是名單也將工作。
如果您使用Python 2,您可能更願意分別使用d.itervalues()d.iteritems()而不是d.values()d.items()

編輯

if語句是不必要的,如果在你的字典鑰匙在實際的職稱,而不是這些g_...字符串。

d = {'Dirt 4': g_dirt4, 'Destiny 2': g_destiny2, 'South Park: The Fractured but Whole': g_southpark, ... } 

max_value = max(d.values()) 
maximal_keys = [ k for k,v in d.items() if v==max_value ] 
for title in maximal_keys: 
    print (title) 

其實這種方式,你可以跳過maximal_key代而直接進入印刷。

max_value = max(d.values()) 
for title, value in d.items(): 
    if value==max_value: 
     print (title) 
+0

非常感謝,它工作:)但是,我怎樣才能將它們打印在一行中呢?使所有可能的陳述將花費太長時間。這樣它就可以檢查maximal_keys中的遊戲,而不用說....在maximal_keys中。就像它自動給你的遊戲。 – Jip1912

+0

@ Jip1912好的,請參閱編輯。 – khelwood

相關問題