Home > other >  how to reduce the time of a nested loop in python
how to reduce the time of a nested loop in python

Time:11-27

I am getting the player data from ESPN, but I find myself with the problem that to get each variable the waiting time is very long, how could I improve the efficiency?

players_by_temp = []
for i in range(20):
    players = []
    for j in range(len(html_table[i].find_all(class_='AnchorLink'))):
        players.append(html_table[i].find_all(class_='AnchorLink')[j].text)
    players_by_temp.append(players)
    print(i)

CodePudding user response:

players_by_temp = []
for i in range(20):
    players = []
    for anchor in html_table[i].find_all(class_='AnchorLink'):
        players.append(anchor.text)
    players_by_temp.append(players)
    print(i)

Once you get more comfortable in Python, you would then replace the three center lines with the following:

    players = [anchor.text for anchor in html_table[i].find_all(...)
  • Related