繁体   English   中英

需要更高效的Python循环

[英]Need a more efficient Python Loop

我目前正在为来自4个不同元组的4个表创建HTML代码,然后打印出整个HTML。 我通过在4个单独的循环中遍历表来做到这一点。 我必须使用4个单独的循环,因为表标签和列引用是不同的。 我正在寻找一些有关提高效率的建议(也许可以组合成一个循环)。 在下面的示例中,每个元组只有1行,因为它只是一个示例,但是当我实际编写此代码时,将有很多行。

我的代码:

datatable1 = [('A', 'B', 'C', 'D', 'E', 'F','G')]
datatable2 = [('H', 'I', 'J', 'K', 'L', 'M','N')]
datatable3 = [('O', 'P', 'Q', 'R', 'S', 'T','U')]
datatable4 = [('W', 'X', 'Y', 'Z')]

HTML_Body1 = "Table1<BR><table>"    
for row in datatable1:
    HTML_Body1 = HTML_Body1 + "<tr><td><font size='2'><td>" + row[0] + "</td><td>" + row[1] + "</td><td><font size='2'>" + row[6] + "</td><td>" + row[4] + "</td></tr>"
HTML_Body1 = HTML_Body1 + "</table><BR><BR>"

HTML_Body2 = "Table2<BR><table>"
for row in datatable2:
    HTML_Body2 = HTML_Body2 + "<tr><td><font size='2'><td>" + row[0] + "</td><td>" + row[1] + "</td><td><font size='2'>" + row[6] + "</td><td>" + row[4] + "</td></tr>"
HTML_Body2 = HTML_Body2 + "</table><BR><BR>"

HTML_Body3 = "Table3<BR><table>"
for row in datatable3:
    HTML_Body3 = HTML_Body3 + "<tr><td><font size='2'><td>" + row[1] + "</td><td>" + row[2] + "</td><td><font size='2'>" + row[3] + "</td><td>" + row[0] + "</td></tr>"
HTML_Body3 = HTML_Body3 + "</table><BR><BR>"

HTML_Body4 = "Table4<BR><table>"    
for row in datatable4:
    HTML_Body4 = HTML_Body4 + "<tr><td><font size='2'><td>" + row[1] + "</td><td>" + row[2] + "</td><td><font size='2'>" + row[3] + "</td><td>" + row[0] + "</td></tr>"
HTML_Body4 = HTML_Body4 + "</table><BR><BR>"

Entire_HTML = "<HMTL>" + HTML_Body1 + HTML_Body2 + HTML_Body3 + HTML_Body4 + "</HTML>"

print Entire_HTML

如果使每个数据表对象稍微复杂一点,则可以更轻松地处理它们。

 datatables = [
     { 'header' : 'Table1', 'rows' : [('A', 'B', 'C', 'D', 'E', 'F','G')], 'want_cols' : (0,1,6,4) },
     { 'header' : 'Table2', 'rows' : [('H', 'I', 'J', 'K', 'L', 'M','N')], 'want_cols' : (0,1,6,4) },
     { 'header' : 'Table3', 'rows' : [('O', 'P', 'Q', 'R', 'S', 'T','U')], 'want_cols' : (1,2,3,0) },
     { 'header' : 'Table4', 'rows' : [('W', 'X', 'Y', 'Z')], 'want_cols' : (1,2,3,0) },
 ]

 output_html = ''
 for tbl in datatables:
      table_html = '{}<br/><table>'.format(tbl['header'])
      for row in tbl['rows']:
          table_html += '\n'.join(
               ['<tr><td>{}</tr></td>'.format(row[index]) for index in tbl['want_cols']]
          )
      table_html += '\n</table>'

      output_html += table_html

可能会有进一步的改进:

  • 用嵌套列表for row in tbl['rows']:替换for row in tbl['rows']:
  • 代替字典,使用具有表头和行属性的DataTable类。
  • 如果数据表的用途主要是表示性的,则可以在类定义中包括asHtmlTable()方法,从而将表组装成更大的HTML样式几乎是声明性的。

第一步,您可以重构表格的头,行和尾

def table_open(title):
    return title + "<BR><table>"

def table_row(d1, d2, d3, d4):
    return "<tr><font size='2'><td>" + d1 + "</td><td>" + d2 + "</td><td><font size='2'>" + d3 + "</td><td>" + d4 + "</td></tr>"

def table_close():
    return "</table><BR><BR>"

并用它代替

tr = ''
for row in datatable1:
    tr = tr + table_row(row[0], row[1], row[6], row[4])

HTML_Body1 = table_open("Table1") + tr + table_close()

一个更紧凑的形式是

tr = [table_row(row[0], row[1], row[6], row[4]) for row in datatable1]
HTML_Body1 = table_open("Table1") + ''.join(tr) + table_close()

它首先捕获行,然后将它们连接起来。

暂无
暂无

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

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