列表理解使用.append

2024-04-20 11:17:27 发布

您现在位置:Python中文网/ 问答频道 /正文

这段代码可能有太多的问题无法在一篇文章中得到回答。我的主要问题是,我正在尝试减少列表理解的for循环的数量。你知道吗

# import required libraries
import bs4, requests, re

# Get the HTML and store it in req variable
req = requests.get('Website')

# Make a BS object to help search HTML
reqSoup = bs4.BeautifulSoup(req.text, 'lxml')

# Search the ATIS under the 'pre' label and store it in reqSoupElems                     
reqSoupElems = reqSoup.select('pre')

# Create Regex's to search the ATIS
timeRegex = re.compile(r'\d\d\d\d\d\d')
WXRegex = re.compile(r'WX:\s.*')

# Create empty lists to store the data extracted for the ATIS
time = []
WX = []

# Create list with strings of time and weather elements extracted from ATIS
for i in range(len(reqSoupElems)):
    # Create time list
    time.append(timeRegex.search(str(reqSoupElems[i])).group())        
    # If the wx group isn't in ATIS append "Nill WX" to WX list
    # Otherwise, append the weather in ATIS to WX list
    if WXRegex.findall(str(reqSoupElems[i])) == []:
        WX.append('Nil WX')
    else:
        WX.append(WXRegex.search(str(reqSoupElems[i])).group())

# Remove "WX: " from the strings within the lists       
for i in range(len(WX)):
    if WX[i][0:4] == 'WX: ':
        WX[i] = WX[i][4:]

# Print to ensure it has worked
print('\n')
print(time)
print(len(time))
print('\n')
print(WX)
print(len(WX))

Tags: thetoinreforsearchlentime
1条回答
网友
1楼 · 发布于 2024-04-20 11:17:27

列表理解并不是唯一的进步。还可以通过直接迭代元素来消除for i in len(range(reqSoupElems))反模式:

time = [timeRegex.search(str(element)).group() for element in reqSoupElems]

相关问题 更多 >