Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. class Placeholder:
  2. def __repr__(self):
  3. return " "
  4.  
  5. empty = Placeholder()
  6. def align_nicely(a,b):
  7. maxlen = lambda seq: max(len(subseq) for subseq in seq)
  8. padded = lambda seq, size: seq + [empty]*(size-len(seq))
  9. maxwidths = [maxlen(a), maxlen(b)]
  10. results = []
  11. for item1, item2 in zip(a,b):
  12. results.append(padded(item1, maxwidths[0]) + padded(item2, maxwidths[1]))
  13. for row in results:
  14. print(row)
  15.  
  16. a = [[1, 2, 3], [1, 2], [1, 2, 3, 4], [1, 2, 3, 4, 5]]
  17. b = [[1, 2, 3], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3]]
  18. align_nicely(a,b)
  19.  
  20. """result:
  21. [1, 2, 3, , , 1, 2, 3, ]
  22. [1, 2, , , , 1, 2, 3, ]
  23. [1, 2, 3, 4, , 1, 2, 3, 4]
  24. [1, 2, 3, 4, 5, 1, 2, 3, ]
  25. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement