Skip to content

Instantly share code, notes, and snippets.

@conath
Created February 3, 2017 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conath/2f41d44e637f114c688f822d20a256dd to your computer and use it in GitHub Desktop.
Save conath/2f41d44e637f114c688f822d20a256dd to your computer and use it in GitHub Desktop.
[Python 2.7] 2d str matrix table-str generation
# Licensed under "The Unlicense"
# Created by CONATH on 03 Feb 2017
def repeatStr(string, times):
ausgabe = str(string)
for i in range(times):
ausgabe += string
return ausgabe
def maxLength(matrix):
imaxLength = 0
for row in range(len(matrix)):
for column in range(len(matrix[row])):
vergl = len(matrix[row][column])
if vergl > imaxLength:
imaxLength = vergl
return imaxLength
def tableStrWith(matrix, noLeadingBlank = False):
numRows = len(matrix)
numCols = len(matrix[0])
cellLength = maxLength(matrix)
tableStr = " " if noLeadingBlank else " "+repeatStr(" ", cellLength)
print("Drucke Adjazenzmatrix fuer {} Knoten!", len(matrix))
for column in range(numCols):
tableStr += "| " + matrix[0][column] + repeatStr(" ", cellLength-len(matrix[0][column]))
for row in range(numRows):
currentRow = " " if noLeadingBlank else " "+matrix[row][0] + repeatStr(" ", cellLength-len(matrix[row][0]))
for column in range(numCols):
wert = matrix[row][column];
currentRow += "| "+ wert + repeatStr(" ", cellLength-len(wert));
tableStr += "\n" if noLeadingBlank else "\n"+repeatStr("-", cellLength);
for column in range(numCols):
tableStr += "-+"+repeatStr("-", cellLength);
tableStr += "\n"+currentRow;
return tableStr
matrix = [["Produkt", "Preis"]]
matrix.append(["Deo", "2,99"])
matrix.append(["Shampoo", "3,29"])
print(matrix)
print(tableStrWith(matrix, True))
print(tableStrWith(matrix, False))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment