查找string是否在变量中

时间:2017-06-12 11:39:58

标签: 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",结果)和(" 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

1 个答案:

答案 0 :(得分: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)