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

时间:2018-06-13 16:03:19

标签: python python-3.x pandas

我有一个包含1,000个URL和公司名称的数据框,我需要将其转换为HTML链接以及进行一些格式化。我编写了一个可以在列表中创建标记的函数:

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

if语句正在进行一些清理工作,因为有几十家公司没有网址。这些在df中表示为'####'和'#####'。对于那些,我添加一个 span标记而不是一个标记,其中一些样式看起来像一个链接。 else语句只根据df中的两列构建链接。

我想做的另一件事是将一半的链接放入,而将后半部分放入。以下是我的代码解释:

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

此代码有效,但这是最有效的方法吗?

1 个答案:

答案 0 :(得分:1)

为了加快速度,您可以先创建一个包含以下链接的列:

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)

然后你的印刷品就像你说的那样不是你的关注:

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

您打印时没有循环列链接的一半