Feb-21-2022, 10:52 AM
(This post was last modified: Feb-21-2022, 10:52 AM by Gribouillis.)
A more flexible version with itertools
import itertools as itt def sandwich(chain, sep): yield from sep for c in chain: yield from c yield from sep def tablerows(r, c): top = ''.join(sandwich((sandwich('-' * n, ' ') for n in c), '+')) mid = ''.join(sandwich((' ' * (2 * n + 1) for n in c), '|')) return sandwich((itt.repeat(mid, k) for k in r), (top,)) a = (3, 2, 4) b = (2, 4, 3, 1) for r in tablerows(a, b): print(r)
Output:+ - - + - - - - + - - - + - +
| | | | |
| | | | |
| | | | |
+ - - + - - - - + - - - + - +
| | | | |
| | | | |
+ - - + - - - - + - - - + - +
| | | | |
| | | | |
| | | | |
| | | | |
+ - - + - - - - + - - - + - +