Advertisement
Guest User

Row-major nested list to latex formatted tabular.

a guest
Sep 1st, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. def ToTable(headers, data, colCount):
  2.     """This method assumes headers is a list, and data is a list of lists.
  3.        Each list in data represents one row of the data column"""
  4.     #Must have at least one column
  5.     colCount = max(1, colCount)
  6.     #LaTeX code for a horizontal line
  7.     h = "\\hline"
  8.     #Escaped slash
  9.     sl = "\\"
  10.     #End of matrix line- the double slash to indicate end of matrix line,
  11.     #a horizontal line for the matrix, and a line return for pretty formatting.
  12.     endl = sl+sl+" "+h+"\n"
  13.  
  14.    
  15.     s = ""
  16.  
  17.     #Start of a matrix
  18.     s += "$latex \\begin{tabular}{"
  19.  
  20.     #Build column layout
  21.  
  22.     #column layout character
  23.     clc = " | c"
  24.     #number of headers per "column"
  25.     nh = len(headers)
  26.  
  27.     #total columns
  28.     tCol = nh*colCount
  29.     #column layout string
  30.     cls = (clc*(tCol+1))[:-1]
  31.     cls += "}"
  32.     #Add the top line
  33.     #Add the column layout
  34.     s += cls
  35.     #Add the top hline, and line return
  36.     s += h + "\n"
  37.    
  38.     #Build headers
  39.     headers_ = ""
  40.     for _ in xrange(colCount):
  41.         for header in headers:
  42.             headers_ += str(header) + " & "
  43.     #Chop the last two characters- "& "
  44.     headers_ = headers_[:-2]
  45.  
  46.     #Add the headers
  47.     s += headers_
  48.     #End the line
  49.     s += endl
  50.    
  51.    
  52.     ld = len(data)
  53.     r = ld / colCount
  54.     #We need an extra row if the rows per column has a fractional amount.
  55.     os = 0
  56.     if (ld % colCount) > 0: os += 1
  57.     #Character to go between matrix column data
  58.     amp = " & "
  59.     #Data Values per "Column" since data[i] may contain [T,U]
  60.     rpc = len(data[0])
  61.  
  62.     rows_ = ""
  63.     #Build each row
  64.     for row in xrange(r+os):
  65.         #Build each column
  66.         for col in xrange(colCount):
  67.             index = row + col*(r+os)
  68.             for i in xrange(rpc):
  69.                 #If we can't add a value...
  70.                 if(index < ld):
  71.                     rows_ += str(data[index][i])
  72.                 #then it's an empty padding spot, so add a space
  73.                 else:
  74.                     rows_ += " "
  75.                 #Append the column separator
  76.                 rows_ += amp
  77.            
  78.         #After finishing a row, remove the last two characters- "& "
  79.         rows_ = rows_[:-2]
  80.         #End the row line
  81.         rows_ += endl
  82.  
  83.     #Add the rows
  84.     s += rows_
  85.     s += " \\end{tabular} $"
  86.  
  87.    
  88.     return s
  89.  
  90.  
  91. headers = ["Depth","Count"]
  92. data = [
  93. [0,1],
  94. [1,2],
  95. [2,3],
  96. [3,5],
  97. [4,8],
  98. [5,13],
  99. [6,19],
  100. [7,30],
  101. [8,41],
  102. [9,57],
  103. [10,83],
  104. [11,114],
  105. [12,155],
  106. [13,216],
  107. [14,293],
  108. [15,394],
  109. [16,536],
  110. [17,724],
  111. [18,971],
  112. [19,1307],
  113. [20,1753],
  114. [21,2354],
  115. [22,3156],
  116. [23,4225],
  117. [24,5668],
  118. [25,7586],
  119. [26,10136],
  120. [27,13542],
  121. [28,18099],
  122. [29,24180],
  123. [30,32309],
  124. [31,43140],
  125. [32,57609],
  126. [33,76902],
  127. [34,102633],
  128. [35,137022],
  129. [36,182860],
  130. [37,244103],
  131. [38,325811],
  132. [39,434816],
  133. [40,580304],
  134. [41,774361],
  135. [42,1033079],
  136. [43,1378432],
  137. [44,1838945],
  138. [45,2453123],
  139. [46,3272729],
  140. [47,4365597],
  141. [48,5823419],
  142. [49,7768300],
  143. [50,10361932],
  144. [51,13821917],
  145. [52,18435615],
  146. [53,24589116],
  147. [54,26770255]
  148.     ]
  149.  
  150. print ToTable(headers, data, 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement