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

时间:2018-11-08 04:19:51

标签: python list function

尝试将列表从一个函数传递到另一个函数(teamurl),然后依次在另一个程序中使用。我的程序在使用yield产生值的地方工作。 (产生full_team_urls) 如何将列表从第一个函数传递到第二个函数(def-team_urls)还可以返回列表并继续使用yield吗? 每个函数只能返回或输出一个对象吗?

编辑:尝试将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()

1 个答案:

答案 0 :(得分:0)

要将值传递给方法,请使用参数:

team_urls(teamurls)

您需要在team_urls的定义中指定该参数,如下所示:

def team_urls(teamurls):