简体   繁体   English

Python-将列表从一个函数传递到另一个函数

[英]Python - Passing a list from one function to another

Trying to pass a list from one function to another (teamurls), which then in turn I will use in another program. 尝试将列表从一个函数传递到另一个函数(teamurls),然后依次在另一个程序中使用。 I have my program working where I have a value output using yield. 我的程序在使用yield产生值的地方工作。 (yield full_team_urls) How do I pass the list from the first function into the second (the def - team_urls) Also is that still possible to return the list and continue using yield aswell? (yield full_team_urls)如何将列表从第一个函数传递到第二个函数(def-team_urls)还可以返回列表并继续使用yield吗? Can each function only return or output one object? 每个函数只能返回或输出一个对象吗?

Edit: Tried to pass teamurls into the second function as shown below and I get the error - TypeError: team_urls() missing 1 required positional argument: 'teamurls'> 编辑:尝试将teamurls传递到第二个函数,如下所示,但出现错误-TypeError:team_urls()缺少1个必需的位置参数:'teamurls'>

def table():
    url = 'https://www.skysports.com/premier-league-table'

    base_url = 'https://www.skysports.com'

    today = str(date.today())

    premier_r = requests.get(url)

    print(premier_r.status_code)

    premier_soup = BeautifulSoup(premier_r.text, 'html.parser')

    headers = "Position, Team, Pl, W, D, L, F, A, GD, Pts\n"

    premier_soup_tr = premier_soup.find_all('tr', {'class': 'standing-table__row'})

    premier_soup_th = premier_soup.find_all('thead')

    f = open('premier_league_table.csv', 'w')
    f.write("Table as of {}\n".format (today))
    f.write(headers)
    premier_soup_tr = premier_soup.find_all('tr', {'class': 'standing-table__row'})
    result = [[r.text.strip() for r in td.find_all('td', {'class': 'standing-table__cell'})][:-1] for td in premier_soup_tr[1:]]
    teamurls = ([a.find("a",href=True)["href"] for a in premier_soup_tr[1:]])
    return teamurls
    for item in result:

        f.write(",".join(item))
        f.write("\n")
    f.close()


    print('\n Premier league teams full urls:\n')
    for item in teamurls:

        entire_team = []

#        full_team_urls.append(base_url+ item)
        full_team_urls = (base_url + item + '-squad')
        yield full_team_urls

table()

def team_urls(teamurls):
    teams = [i.strip('/') for i in teamurls]
    print (teams)
team_urls()

To pass a value into a method, use arguments: 要将值传递给方法,请使用参数:

team_urls(teamurls)

You'll need to specify that argument at the definition of team_urls as such: 您需要在team_urls的定义中指定该参数,如下所示:

def team_urls(teamurls):

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

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