Python-获取具有最高整数的变量名

Python-获取具有最高整数的变量名,python,Python,在向变量中添加数字后,如何获得整数最高的变量 g_dirt4 = 0 g_destiny2 = 0 g_southpark = 0 g_codww2 = 0 g_bfront2 = 0 g_reddead2 = 0 g_fifa18 = 0 g_motogp17 = 0 g_elderscrolls = 0 g_crashbandicoot = 0 #Right here are a lot of if statements which add += 1 to the specific var

在向变量中添加数字后,如何获得整数最高的变量

g_dirt4 = 0
g_destiny2 = 0
g_southpark = 0
g_codww2 = 0
g_bfront2 = 0
g_reddead2 = 0
g_fifa18 = 0
g_motogp17 = 0
g_elderscrolls = 0
g_crashbandicoot = 0

#Right here are a lot of if statements which add += 1 to the specific variables

if request.method == "POST":
    #I would like it to print like this:
    print(Highest variable name, corresponding number)
我该怎么做

编辑:

这是我的全部剧本

from flask import Flask, render_template, request, redirect

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route("/", methods=["GET", "POST"])



def main():

    g_dirt4 = 0
    g_destiny2 = 0
    g_southpark = 0
    g_codww2 = 0
    g_bfront2 = 0
    g_reddead2 = 0
    g_fifa18 = 0
    g_motogp17 = 0
    g_elderscrolls = 0
    g_crashbandicoot = 0

    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}

    if request.method == "GET":
        return render_template("main_page.html")

    if (request.form["console"] == "PC"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["console"] == "PS4"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["console"] == "Xbox One"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["console"] == "Maakt niet uit"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["leeftijd"] == "Vanaf 3 jaar"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["leeftijd"] == "Vanaf 7 jaar"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["leeftijd"] == "Vanaf 12 jaar"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["leeftijd"] == "Vanaf 16 jaar"):
        g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["leeftijd"] == "Vanaf 18 jaar"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["genre"] == "Shooter"):
        #g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["genre"] == "Sports"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["genre"] == "Role-playing"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["genre"] == "Platform"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["genre"] == "Simulation"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["multiplayer"] == "Ja"):
        g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["multiplayer"] == "Nee"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["splitscreen"] == "Ja"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["splitscreen"] == "Nee"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["geweld"] == "Ja"):
        #g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["geweld"] == "Nee"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["interesse"] == "balsport"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["interesse"] == "motorsport"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["interesse"] == "geschiedenis/oorlog"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        g_codww2 += 1
        #g_bfront2 += 1
        g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["interesse"] == "films/series"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        g_southpark += 1
        #g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["interesse"] == "fantasie/fictie"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["interesse"] == "avonturen"):
        #g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["competitief"] == "Ja"):
        g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["competitief"] == "Nee"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["moeteengoeie"] == "single player storyline hebben"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        #g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["moeteengoeie"] == "single player open-world hebben"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        #g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["moeteengoeie"] == "multiplayer storyline hebben"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["moeteengoeie"] == "multiplayer open-world hebben"):
        #g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        g_reddead2 += 1
        #g_fifa18 += 1
        #g_motogp17 += 1
        g_elderscrolls += 1
        #g_crashbandicoot += 1

    if (request.form["releasedatum"] == "Juni 2017"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["releasedatum"] == "Juli 2017"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["releasedatum"] == "Augustus 2017"):
        g_dirt4 += 1
        #g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        #g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["releasedatum"] == "September 2017"):
        g_dirt4 += 1
        g_destiny2 += 1
        #g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["releasedatum"] == "Oktober 2017"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        #g_codww2 += 1
        #g_bfront2 += 1
        #g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1

    if (request.form["releasedatum"] == "November 2017 of later"):
        g_dirt4 += 1
        g_destiny2 += 1
        g_southpark += 1
        g_codww2 += 1
        g_bfront2 += 1
        g_reddead2 += 1
        g_fifa18 += 1
        g_motogp17 += 1
        g_elderscrolls += 1
        g_crashbandicoot += 1



    if request.method == "POST":
        print(max(d, key=d.get))
        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)
        return redirect("https://i.vimeocdn.com/portrait/8487168_300x300")
我不知道是否有更简单的方法来实现这一点,但这对我来说也很有效。这确实花了一些时间。

使用字典:

g_dirt4 = 0
g_destiny2 = 0
g_southpark = 0
...

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

print(max(d.items(), key=lambda x: x[1]))
使用字典:

g_dirt4 = 0
g_destiny2 = 0
g_southpark = 0
...

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

print(max(d.items(), key=lambda x: x[1]))

max(g_dirt4,g_destiny2,…)
?不,我还需要这个名字。
max(g_dirt4,g_destiny2,…)
?不,我还需要这个名字。有没有理由把它们作为单独的变量放在第一位?在我看来,g_u前缀是试图创建某种形式的任意数据模型。如果把它们放在字典里,并在需要的时候从字典中引用,那会更有意义。我同意@JamoBox的评论。但是,如果无法更改现有代码,可以这样做:num,var_name=max([(v,k)表示局部变量中的k,v().items(),如果k.startswith('g_')])这将获取与所有局部变量关联的字典,选择具有g_前缀的变量,并查找其值具有与其关联的最大数字的变量。
max(d,key=d.get)
将起作用。它避免了不必要的缓慢
lambda
调用和项目视图/列表创建。感谢您的回答,但它实际上不起作用。日志返回:2017-06-04 18:04:49('g_dirt4',0)2017-06-04 18:04:49 g_dirt4 6 g_destiny 2 g_south park 6 g_codww 2 g_bfront 2 g_reddead2 g_2 g_fifa18 g_motogp 7 g_elders crolls 6 g_crashbandicoot 6。这是说g_destiny的分数为8,这是最高的,但它也说dirt4的分数为0是最高的,但dirt4的分数为6。那么,你必须为你的问题提供更多的背景。我只是给你们展示了一个数据结构的例子,它应该能解决这类问题。有没有理由把它们作为单独的变量放在首位?在我看来,g_u前缀是试图创建某种形式的任意数据模型。如果把它们放在字典里,并在需要的时候从字典中引用,那会更有意义。我同意@JamoBox的评论。但是,如果无法更改现有代码,可以这样做:num,var_name=max([(v,k)表示局部变量中的k,v().items(),如果k.startswith('g_')])这将获取与所有局部变量关联的字典,选择具有g_前缀的变量,并查找其值具有与其关联的最大数字的变量。
max(d,key=d.get)
将起作用。它避免了不必要的缓慢
lambda
调用和项目视图/列表创建。感谢您的回答,但它实际上不起作用。日志返回:2017-06-04 18:04:49('g_dirt4',0)2017-06-04 18:04:49 g_dirt4 6 g_destiny 2 g_south park 6 g_codww 2 g_bfront 2 g_reddead2 g_2 g_fifa18 g_motogp 7 g_elders crolls 6 g_crashbandicoot 6。这是说g_destiny的分数为8,这是最高的,但它也说dirt4的分数为0是最高的,但dirt4的分数为6。那么,你必须为你的问题提供更多的背景。我只是向您展示一个数据结构的示例,它应该适用于这类问题。