Wpis z mikrobloga

Kurła, no chyba mi się mózg przegrzał. Siedzę nad tym od pół godziny a to przecież wygląda banalnie. Pomoże ktoś?

Write a function named printTable() that takes a list of lists of strings and displays it in a well-organized table with each column right-justified. Assume that all the inner lists will contain the same number of strings. For example, the value could look like this:

tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]

Your printTable() function would print the following:

apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose

#python
#programowanie
  • 9
  • Odpowiedz
@programista_1k:

tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]

transpose = list(map(list, zip(*tableData)))

def printTable(table):
[print(" ".join(x)) for x in table]

printTable(transpose)
  • Odpowiedz
@Defined:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]

def printTable(table):
....string = ""
....i = 0
....tableWidth = len(table[0])
....tableHeight = len(table)
....while i <= tableHeight:
........for n in range(tableWidth - 1):
............string = string + table[n][i] + ' '
........print(string)
........i += 1
........string = ""

printTable(tableData)

Tak żem to zrobił, po Twojemu nie działa ale nakierowałeś mnie, dzięki ;)
  • Odpowiedz