简体   繁体   中英

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. I am doing this by looping through the tables in 4 separate loops. I have to use 4 separate loops because the table labels and column references are different. 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.

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.
  • Instead of dictionaries, use a DataTable class with header and rows properties.
  • 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.

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.

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