查找字符串是否在variab中

2024-10-04 03:27:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个脚本来获取具有最高值+它的名称的变量。如果有更多的变量具有相同的最高变量,那么它将返回所有这些变量。如果你不明白我在说什么,这就是我的意思: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”,result)和(“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


Tags: in脚本ifresultprintdestiny2crashbandicootsouthpark
1条回答
网友
1楼 · 发布于 2024-10-04 03:27:46

下面是如何在字典中找到映射到最大值的键集。你知道吗

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使用了一个集合,因为它对查找更有效,但是列表也可以工作。
如果您使用的是python2,那么您可能更喜欢使用^{}^{},而不是分别使用^{}^{}。你知道吗

编辑

如果词典中的键是实际的标题,而不是这些g_...字符串,那么if语句将是不必要的。你知道吗

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)

相关问题 更多 >