Python 查找字符串是否在变量中

Python 查找字符串是否在变量中,python,Python,我有这个脚本来获取具有最高值+它的名称的变量。如果有更多的变量具有相同的最高变量,那么它将返回所有变量。如果你不明白我在说什么,这就是我的意思: 脚本 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'

我有这个脚本来获取具有最高值+它的名称的变量。如果有更多的变量具有相同的最高变量,那么它将返回所有变量。如果你不明白我在说什么,这就是我的意思:

脚本

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')
......
我想看看结果是哪场比赛。我该怎么做

编辑:


完整脚本

以下是如何在字典中找到映射到最大值的键集

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.
我为
最大\u键使用了一个集合,因为它对查找更有效,但是列表也可以使用。
如果您使用的是Python2,您可能更喜欢分别使用and而不是and

编辑

如果词典中的键是实际的标题,而不是这些字符串,
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_键
生成,直接进行打印

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

您正在将
result
构建为成对的列表。然后检查各种字符串是否是列表的元素(即,是否等于这些对中的任何一对)。字符串不等于对,因此您的
if
s将不匹配。也许你想建立一个键列表而不是一个键对列表,就像我说的:Resultaat[('g_dirt4',8),('g_motogp17',8)]这些变量在哪里声明<代码>g_dirt4,…
使用完整代码的粘贴库更改了线程。我觉得没必要,我该怎么做@克尔伍德非常感谢,它是有效的:)但实际上我如何把它们打印成一行呢?如果陈述的时间太长,那么尽可能多地进行陈述。这样它就可以检查最大_键中有哪些游戏,而不用说。。。。以最大的U键。就像它自动给你的游戏。