如何减少 python 中嵌套循环的时间

how to reduce the time of a nested loop in python

我正在从ESPN获取球员数据,但我发现自己的问题是获取每个变量等待时间很长,如何提高效率?

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)
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)

一旦您对 Python 感到更舒服,您就可以将三个中心线替换为以下内容:

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