简体   繁体   中英

Making Python for loop with nested if statement more efficient

I have a dataframe with 1,000s of URLs and company names that I need to convert to HTML links as well as do some formatting. I wrote a function that can go down the list and create the tags:

def linkcreate():
    if row['url'] == '####' or row['url'] == '#####':
        print('<span style="color: #293789!important; margin-bottom:0;">' + row['name'] + '</span>')
    else:
        print('<a href="' + row['url'] + '" target="_blank">' + row['name'] + '</a>')

The if statement is doing a bit of a clean up since there are a few dozen companies that do not have a url. Those are represented as '####' and '#####' in the df. For those, I am adding a span tag instead of a tag with some styling that will look like a link. else statement just constructs the link based on two columns in the df.

Another thing I wanted to do was put the half of the links in and the second half in . Below is my code with explanation:

# Calculates the middle point from the total count of rows in df
count = (int(data['url'].count()))/2
# Set counter to 0
counter = 0

for index, row in data.iterrows():
    counter = counter + 1
# Before the first <a> tag start the first section <div>
    if counter == 1:
        print('<div class="side-1">')
# If counter is less then or equals to the half point of rows in df build the links using the linkcreate()
    elif counter <= count:
        linkcreate()
# If counter is +1 from the half way point of rows add the closing </div> and start the second <div>
    elif counter == count + 1:
        print('</div>')
        print(' ')
        print('<div class="side-2">')
# If counter is greater then the half point of rows in df build the rest of the links using the linkcreate()
    elif counter > count:
        linkcreate()
# Closing </div> tag for the second set of links.
print('</div>')

This code works but is it the most efficient way to do this?

To be faster, you can first create a column with the links:

def linkcreate(row):
    if '####' in row['url']: # will catch both '####' and '#####'
        return '<span style="color: #293789!important; margin-bottom:0;">' + row['name'] + '</span>'
    else:
        return '<a href="' + row['url'] + '" target="_blank">' + row['name'] + '</a>'
df['link'] = df.apply(linkcreate,axis=1)

Then your print as you said it's not your concern:

print('<div class="side-1">')
print(df['link'][:count+1].to_string(header=None, index=False))
print('</div>')
print(' ')
print('<div class="side-2">')
print(df['link'][count+1:].to_string(header=None, index=False))
print('</div>')

you print without loop half of your column link

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