0

I'm having an issue keeping a certain format when printing items in my list:

[TEXT1, TEXT2, TEXT3, TEXT4, TEXT5, TEXT6, TEXT7, TEXT8, TEXT9]

Expected Output: ('~' represents whitespace in the document)

~~~~~~~~~~TEXT1, TEXT2, TEXT3, TEXT4, TEXT5,
~~~~~~~~~~TEXT6, TEXT7, TEXT8, TEXT9

The issue I'm experiencing is appending TEXT2 after TEXT1, without re-adding the whitespace('~').

a = ['A121', 'A221', 'A321', 'A421', 'A521', 'A621', 'A721', 'A821', 'A921']

for v in a:
    counter = 0
    if counter == 5:
        print '\n'
    print "                {},".format(v.ljust(0)),

Output:

                A121                 A221                 A321                 A421                 A521                 A621                 A721                 A821                 A921

Expected output:

                A121, A221, A321, A421, A521,
                A621, A721, A821, A921

2 Answers 2

1

There are many ways to tackle this problem:

One:

a = ['A121', 'A221', 'A321', 'A421', 'A521', 'A621', 'A721', 'A821', 'A921']

INDENT = ' '*10
NUMBER_OF_COLUMNS = 5

lead = INDENT
for item_number, item in enumerate(a):
    print("{lead}{item}".format(lead=lead, item=item), end='')

    if item_number % NUMBER_OF_COLUMNS == NUMBER_OF_COLUMNS - 1:
        lead = ",\n" + INDENT
    else:
        lead = ", "

Two:

table = []
row = []

for item in a:
    row.append(item)
    if len(row) == NUMBER_OF_COLUMNS:
        table.append(INDENT + ', '.join(row))
        row = []

table.append(INDENT + ', '.join(row))

print(',\n'.join(table))

Three, a one-liner:

print(',\n'.join(INDENT + ', '.join(a[i:i + NUMBER_OR_COLUMNS]) for i in range(0, len(a), NUMBER_OR_COLUMNS)))

Or perhaps textwrap.fill() would work:

from textwrap import fill

print(fill(', '.join(a), initial_indent=10*' ', subsequent_indent=10*' ', width=40))

All output (with the commas a shown in your example):

          A121, A221, A321, A421, A521,
          A621, A721, A821, A921
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your awesome explanation!
0

Here's a fairly generic, succinct, and efficient way to do it:

import textwrap
from itertools import zip_longest

def grouper(n, iterable, _SENTINEL=object()):
    """ Collect data into fixed-length chunks or blocks. """
    args = [iter(iterable)] * n
    for t in zip_longest(*args, fillvalue=_SENTINEL):
        yield list(elem for elem in t if elem is not _SENTINEL)

def text_wrap(words, numcols, indent=0):
    lines = (' '.join(group) for group in grouper(numcols, words))
    return textwrap.indent('\n'.join(lines), indent*' ')


if __name__ == '__main__':

    a = 'A121', 'A221', 'A321', 'A421', 'A521', 'A621', 'A721', 'A821', 'A921'
    lines = text_wrap(a, 5, 8)
    print(lines)

Output:

        A121 A221 A321 A421 A521
        A621 A721 A821 A921

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.