简体   繁体   English

需要更高效的Python循环

[英]Need a more efficient Python Loop

I am currently creating HTML code for 4 tables from 4 different tuples then printing out the entire HTML. 我目前正在为来自4个不同元组的4个表创建HTML代码,然后打印出整个HTML。 I am doing this by looping through the tables in 4 separate loops. 我通过在4个单独的循环中遍历表来做到这一点。 I have to use 4 separate loops because the table labels and column references are different. 我必须使用4个单独的循环,因为表标签和列引用是不同的。 I'm looking for some suggestions on improving the efficiency of this (maybe combining down to one loop). 我正在寻找一些有关提高效率的建议(也许可以组合成一个循环)。 In the below example, each tuple only has 1 row since it's just a sample, but when I actually code this, there will be many rows. 在下面的示例中,每个元组只有1行,因为它只是一个示例,但是当我实际编写此代码时,将有很多行。

My code: 我的代码:

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

If you make each datatable object a little more complex you can process them more easily. 如果使每个数据表对象稍微复杂一点,则可以更轻松地处理它们。

 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

Further improvements are possible: 可能会有进一步的改进:

  • Replace for row in tbl['rows']: with a nested list comprehension. 用嵌套列表for row in tbl['rows']:替换for row in tbl['rows']:
  • Instead of dictionaries, use a DataTable class with header and rows properties. 代替字典,使用具有表头和行属性的DataTable类。
  • If the purpose of the data tables is primarily presentational you could include an asHtmlTable() method in the class definition, making assembling the tables into a larger piece of HTML almost declarative in style. 如果数据表的用途主要是表示性的,则可以在类定义中包括asHtmlTable()方法,从而将表组装成更大的HTML样式几乎是声明性的。

As a first step, you could refactor the head, rows and tail of the table 第一步,您可以重构表格的头,行和尾

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>"

and use this instead 并用它代替

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

A more compact form would be 一个更紧凑的形式是

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

which captures the rows first and concatenates them afterwards. 它首先捕获行,然后将它们连接起来。

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

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