Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def ToTable(headers, data, colCount):
- """This method assumes headers is a list, and data is a list of lists.
- Each list in data represents one row of the data column"""
- #Must have at least one column
- colCount = max(1, colCount)
- #LaTeX code for a horizontal line
- h = "\\hline"
- #Escaped slash
- sl = "\\"
- #End of matrix line- the double slash to indicate end of matrix line,
- #a horizontal line for the matrix, and a line return for pretty formatting.
- endl = sl+sl+" "+h+"\n"
- s = ""
- #Start of a matrix
- s += "$latex \\begin{tabular}{"
- #Build column layout
- #column layout character
- clc = " | c"
- #number of headers per "column"
- nh = len(headers)
- #total columns
- tCol = nh*colCount
- #column layout string
- cls = (clc*(tCol+1))[:-1]
- cls += "}"
- #Add the top line
- #Add the column layout
- s += cls
- #Add the top hline, and line return
- s += h + "\n"
- #Build headers
- headers_ = ""
- for _ in xrange(colCount):
- for header in headers:
- headers_ += str(header) + " & "
- #Chop the last two characters- "& "
- headers_ = headers_[:-2]
- #Add the headers
- s += headers_
- #End the line
- s += endl
- ld = len(data)
- r = ld / colCount
- #We need an extra row if the rows per column has a fractional amount.
- os = 0
- if (ld % colCount) > 0: os += 1
- #Character to go between matrix column data
- amp = " & "
- #Data Values per "Column" since data[i] may contain [T,U]
- rpc = len(data[0])
- rows_ = ""
- #Build each row
- for row in xrange(r+os):
- #Build each column
- for col in xrange(colCount):
- index = row + col*(r+os)
- for i in xrange(rpc):
- #If we can't add a value...
- if(index < ld):
- rows_ += str(data[index][i])
- #then it's an empty padding spot, so add a space
- else:
- rows_ += " "
- #Append the column separator
- rows_ += amp
- #After finishing a row, remove the last two characters- "& "
- rows_ = rows_[:-2]
- #End the row line
- rows_ += endl
- #Add the rows
- s += rows_
- s += " \\end{tabular} $"
- return s
- headers = ["Depth","Count"]
- data = [
- [0,1],
- [1,2],
- [2,3],
- [3,5],
- [4,8],
- [5,13],
- [6,19],
- [7,30],
- [8,41],
- [9,57],
- [10,83],
- [11,114],
- [12,155],
- [13,216],
- [14,293],
- [15,394],
- [16,536],
- [17,724],
- [18,971],
- [19,1307],
- [20,1753],
- [21,2354],
- [22,3156],
- [23,4225],
- [24,5668],
- [25,7586],
- [26,10136],
- [27,13542],
- [28,18099],
- [29,24180],
- [30,32309],
- [31,43140],
- [32,57609],
- [33,76902],
- [34,102633],
- [35,137022],
- [36,182860],
- [37,244103],
- [38,325811],
- [39,434816],
- [40,580304],
- [41,774361],
- [42,1033079],
- [43,1378432],
- [44,1838945],
- [45,2453123],
- [46,3272729],
- [47,4365597],
- [48,5823419],
- [49,7768300],
- [50,10361932],
- [51,13821917],
- [52,18435615],
- [53,24589116],
- [54,26770255]
- ]
- print ToTable(headers, data, 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement