简体   繁体   中英

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. I have my program working where I have a value output using 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? 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'>

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:

def team_urls(teamurls):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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