简体   繁体   中英

List comprehension using .append

There is probably so many problems with this code that can't be answered in a single post. My main problem is I'm trying to lessen the amount of for loops with list comprehensions.

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

list comprehension isn't the only mprovement. Also get rid of for i in len(range(reqSoupElems)) antipattern by iterating directly on the elements:

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

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