1

I have a 2D array (I know it is strictly a list of list, but for my purposes, the term 2D array is fine to use) called results full of strings that I want to display in a nice "lined-up" format. Bear in mind that I really don't want to use any imported modules here.

My first attempt of:

for i in range (len(results)):
    line = results[i]
    print(line[0].ljust(30), line[1], line[2])

made everything incredibly jarred due to letters (and punctucation etc.) being different widths.

My second attempt of:

for i in range (len(results)):
    line = results[i]
    foo = line[0]
    foo = foo.ljust(len(foo)+(7-len(foo)//9), "\t")
    print(foo, line[1], line[2])

worked a lot better, however for around 1 in 10 lines (basically any string with length +/- 1 a multiple of 9), it would add one too many or one too little tabs.

What would be the best way of going about this problem?

1 Answers1

0

[Edited to reflect the discussion below]

If your letters and punctuation are different widths, then you can really only expect alignment if you either (1) count characters and use a monospace typeface or (2) count characters weighted by their widths and then add the right number of tabs to align the next column.

Approach 2 is trickier, so simply using your first approach with a typeface that doesn't violate the assumption of same-width characters should be sufficient.

Pavel Komarov
  • 1,153
  • 12
  • 26
  • You can see quite clearly that things like numpy.array([["bananas",2,3],[4,"coffee",6],["short",8,"very-very-long"]]) aren't aligned – Alexander51413 Mar 17 '18 at 01:13
  • You do say you would rather not depend on external libraries, but numpy has set_printoptions https://stackoverflow.com/questions/2891790/how-to-pretty-printing-a-numpy-array-without-scientific-notation-and-with-given It might not work if you're mixing in strings. – Pavel Komarov Mar 17 '18 at 01:23
  • If your letters and punctuation are different widths, then where are you even printing this? You can really only expect alignment if you either (1) count characters and use a monospace font or (2) count characters weighted by their widths and then add just the right number of spaces to make up for some disparity, which might not be possible because spaces have some width that will make any adjustments granular. – Pavel Komarov Mar 17 '18 at 01:26
  • So assuming I'm not doing option (1), are you telling me that this is impossible (or at least practically infeasible)? – Alexander51413 Mar 17 '18 at 01:28
  • If you use tabs, then even in a not-monospace typeface you could potentially add enough of them to align yourself, but calculating how many you need would get difficult because it depends on the character widths in addition to just the number of characters. I'd say a monospace font might work better. Does your first approach appear better with Courier? – Pavel Komarov Mar 17 '18 at 01:30
  • Yes it does, and it seems like its the only option available, so i'll have to go with that – Alexander51413 Mar 17 '18 at 01:52