简体   繁体   English

我如何从 Python 函数返回多个变量

[英]How do i return multiples variables from Python function

I have a function that extracts a string from a website, the function is as follows:我有一个从网站中提取字符串的函数,函数如下:

def get_maps_info(url):
page = get_parsed_page(url)
# extract match infos 
maps = page.find('div', {'class' : 'padding preformatted-text'})
return maps.text

The code above will returning something like this:上面的代码将返回如下内容:

'Best of 1\n\n* Group B elimination match'

So I have another function that checks, if find 'Best of 3' starts the first 'if', i am very newbie in python, but i think this is ok, if u have some tips or best practice please show me.所以我有另一个函数来检查,如果找到 'Best of 3' 从第一个 'if' 开始,我是 python 的新手,但我认为这没问题,如果你有一些提示或最佳实践,请告诉我。

The problem is i need to return all this variables, how i can do this?问题是我需要返回所有这些变量,我该怎么做?

def get_results_maps(url):
page = get_parsed_page(url)

if 'Best of 3' in get_maps_info(url):
    ## verify team winners for each map for Best of 3
    ## first team is the winner and second is the loser and so on
    if int(page.findAll('div', {'class' : 'results-team-score'})[0].text)>=int(page.findAll('div', {'class' : 'results-team-score'})[1].text):
        team_winner_map1 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[0].text) 
        team_loser_map1 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[1].text)   
    else:
        team_winner_map1 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[1].text)
        team_loser_map1 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[0].text) 

    if int(page.findAll('div', {'class' : 'results-team-score'})[2].text)>=int(page.findAll('div', {'class' : 'results-team-score'})[3].text):
        team_winner_map2 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[0].text)
        team_loser_map2 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[1].text)
    else:
        team_winner_map2 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[1].text)
        team_loser_map2 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[0].text)

    if int(page.findAll('div', {'class' : 'results-team-score'})[4].text)>=int(page.findAll('div', {'class' : 'results-team-score'})[5].text):
        team_winner_map3 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[0].text)
        team_loser_map3 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[1].text) 
    else:
        team_winner_map3 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[1].text)
        team_loser_map3 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[0].text)

    # end verify team winners for each map for Best of 3

    ## verify team winners for each map for Best of 1
elif 'Best of 1' in get_maps_info(url):
    if int(page.findAll('div', {'class' : 'results-team-score'})[0].text)>=int(page.findAll('div', {'class' : 'results-team-score'})[1].text):
        team_winner_map1 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[0].text) 
        team_loser_map1 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[1].text)   
    else:
        team_winner_map1 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[1].text)
        team_loser_map1 = (page.findAll('div', {'class' : 'results-teamname text-ellipsis'})[0].text) 
    # end verify team winners for each map for Best of 1    

return (team_winner_map1, team_loser_map1, team_winner_map2, team_loser_map2, team_winner_map3, team_loser_map3)

error after i run the function above:运行上面的函数后出错:

错误

I don't know if I was clear enough, otherwise feel free to ask about anything to me.我不知道我是否足够清楚,否则随时向我询问任何事情。

The issue you have is that not all of the variables you're using in your return statement are created in the code above.您遇到的问题是,并非您在return语句中使用的所有变量都是在上面的代码中创建的。 There are a few ways that can happen.有几种可能发生的方式。

To begin with, if neither Best of 1 nor Best of 3 appear, you won't define any of the variables.首先,如果Best of 1Best of 3均未出现,则您将不会定义任何变量。 That's obviously bad, and might indicate that your data isn't entirely in the format you expect.这显然很糟糕,并且可能表明您的数据并不完全符合您期望的格式。 You might diagnose that situation by adding an else section to your existing if / elif branches.您可以通过向现有if / elif分支添加else部分来诊断这种情况。 Even if it's not supposed to ever happen, it's often worth checking every possibility and at least reporting the error in a good way.即使它不应该发生,通常也值得检查每一种可能性,至少以一种好的方式报告错误。

The other issue is that your if and elif options define different sets of variables.另一个问题是您的ifelif选项定义了不同的变量集。 Your current code expects all three pairs of winning and losing scores, but in the Best of 1 case, it will only find the first pair.您当前的代码期望所有三对输赢分数,但在Best of 1情况下,它只会找到第一对。 To fix this, you either need to customize the return statement for each branch (so the best of 1 case can return a 2-tuple rather than a 6-tuple), or you need to define dummy values for the games that never occurred.要解决此问题,您要么需要为每个分支自定义return语句(因此最好的 1 种情况可以返回 2 元组而不是 6 元组),或者您需要为从未发生过的游戏定义虚拟值。 (I'm also not sure if you'll always get three game results in a best of 3, but that's a different issue.) (我也不确定你是否总是能在三局两胜的情况下获得三局比赛的结果,但这是一个不同的问题。)

A minor issue (that might or might not be a big deal, depending on how code you haven't shown is implemented) is that you're repeatedly calling the get_maps_info function in the different tests for your if / elif That may be inefficient if it needs to fetch the URL from the web each time.一个小问题(这可能是也可能不是什么大问题,取决于您未显示的代码是如何实现的)是您在if / elif的不同测试中反复调用get_maps_info函数如果它每次都需要从网络上获取 URL。 A better approach might be to save the results of just one call and reuse it as necessary (as you're already doing with page ).更好的方法可能是保存一次调用的结果并在必要时重用它(正如您已经在使用page所做的那样)。

Putting this all together, I'd structure the code something more like:将所有这些放在一起,我会更像构建代码:

page = get_parsed_page(url)
maps_info = get_maps_info(url)

if 'Best of 3' in maps_info:
    ...     # code to find winner/loser stuff omitted for brevity

    return (team_winner_map1, team_loser_map1,
            team_winner_map2, team_loser_map2,
            team_winner_map3, team_loser_map3) # move the current return statement here!

elif 'Best of 1' in maps_info:
    ...

    return (team_winner_map1, team_loser_map1) # return a 2-tuple in this case

else:  # report error if we don't get any of the strings we expect
    raise ValueError("Expected Best of 3 or Best of 1, got map-info: {!r}".format(map_info))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM