简体   繁体   English

使用嵌套的if语句使Python for循环更有效

[英]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. 我有一个包含1,000个URL和公司名称的数据框,我需要将其转换为HTML链接以及进行一些格式化。 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. if声明正在进行一些清理,因为有几十家公司没有网址。 Those are represented as '####' and '#####' in the df. 这些在df中表示为'####'和'#####'。 For those, I am adding a span tag instead of a tag with some styling that will look like a link. 对于那些,我添加一个span标签而不是标签 ,其中一些样式看起来像一个链接。 else statement just constructs the link based on two columns in the df. else语句只是根据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 你打印没有循环列链接的一半

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM