简体   繁体   中英

Print list of lists in an 'elegant' way

I have the following list of lists:

poster = [[1,  'Col',  1, 28, 29, 30, 19,      0,     0,     0,    0, 0, 0, 0, 0, 0,                  0],
          [2,  'Col',  1, 31, 32, 33, 22,      0,     0,     0,    0, 0, 0, 0, 0, 0,                  0],
          [3,  'Col',  1, 34, 35, 36, 25,      0,     0,     0,    0, 0, 0, 0, 0, 0,                  0],
          [4,  'Col',  2, 19, 20, 21, 10,      0,     0,     0,    0, 0, 0, 0, 0, 0,                  0],
          [5,  'Col',  2, 22, 23, 24, 13,      0,     0,     0,    0, 0, 0, 0, 0, 0,                  0],
          [6,  'Col',  2, 25, 26, 27, 16,      0,     0,     0,    0, 0, 0, 0, 0, 0,                  0],
          [7,  'Col',  3, 10, 11, 12,  1,      0,     0,     0,    0, 0, 0, 0, 0, 0,                  0],
          [8,  'Col',  3, 13, 14, 15,  4,      0,     0,     0,    0, 0, 0, 0, 0, 0,                  0],
          [9,  'Col',  3, 16, 17, 18,  7,      0,     0,     0,    0, 0, 0, 0, 0, 0,                  0],
          [10, 'Row',  1, 19, 20, 21, 22,  -3660, -1500, -6792,    0, 0, 0, 0, 0, 3777.5121428571438, 0],
          [11, 'Row',  1, 22, 23, 24, 25,  -3660, -1500, -6792,    0, 0, 0, 0, 0, 0,                  0],
          [12, 'Row',  2, 10, 11, 12, 13,  -3660, -1500, -6792,    0, 0, 0, 0, 0, 7555.0242857142875, 0],
          [13, 'Row',  2, 13, 14, 15, 16,  -3660, -1500, -6792,    0, 0, 0, 0, 0, 0,                  0],
          [14, 'Row',  3,  1,  2,  3,  4,  -3660, -1500, -6792,    0, 0, 0, 0, 0, 11332.536428571431, 0],
          [15, 'Row',  3,  4,  5,  6,  7,  -3660, -1500, -6792,    0, 0, 0, 0, 0, 0,                  0]]

What I want is to print it on the screen in the following format:

[[ 1, 'Col',  1, 28, 29, 30, 19,      0,     0,     0,    0, 0, 0, 0, 0,                  0,   0],
 [ 2, 'Col',  1, 31, 32, 33, 22,      0,     0,     0,    0, 0, 0, 0, 0,                  0,   0],
 [ 3, 'Col',  1, 34, 35, 36, 25,      0,     0,     0,    0, 0, 0, 0, 0,                  0,   0],
 [ 4, 'Col',  2, 19, 20, 21, 10,      0,     0,     0,    0, 0, 0, 0, 0,                  0,   0],
 [ 5, 'Col',  2, 22, 23, 24, 13,      0,     0,     0,    0, 0, 0, 0, 0,                  0,   0],
 [ 6, 'Col',  2, 25, 26, 27, 16,      0,     0,     0,    0, 0, 0, 0, 0,                  0,   0],
 [ 7, 'Col',  3, 10, 11, 12,  1,      0,     0,     0,    0, 0, 0, 0, 0,                  0,   0],
 [ 8, 'Col',  3, 13, 14, 15,  4,      0,     0,     0,    0, 0, 0, 0, 0,                  0,   0],
 [ 9, 'Col',  3, 16, 17, 18,  7,      0,     0,     0,    0, 0, 0, 0, 0,                  0,   0],
 [10, 'Row',  1, 19, 20, 21, 22,  -3660, -1500, -6792,    0, 0, 0, 0, 0, 3777.5121428571438,   0],
 [11, 'Row',  1, 22, 23, 24, 25,  -3660, -1500, -6792,    0, 0, 0, 0, 0,                  0,   0],
 [12, 'Row',  2, 10, 11, 12, 13,  -3660, -1500, -6792,    0, 0, 0, 0, 0, 7555.0242857142875,   0],
 [13, 'Row',  2, 13, 14, 15, 16,  -3660, -1500, -6792,    0, 0, 0, 0, 0,                  0,   0],
 [14, 'Row',  3,  1,  2,  3,  4,  -3660, -1500, -6792,    0, 0, 0, 0, 0, 11332.536428571431,   0],
 [15, 'Row',  3,  4,  5,  6,  7,  -3660, -1500, -6792,    0, 0, 0, 0, 0,                  0,   0]]

I have tried with a liner, I have succeeded but above all I am looking for it to be ordered, that is, for the columns to be aligned, since in this case this list of lists is large.

print('\n'.join(', '.join(map(str,sl)) for sl in poster))

Best regards.

One easy way to to that is to load in into a pandas dataframe:

import pandas as pd
pd.DataFrame(poster)

Output:

    0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16
0   1   Col     1   28  29  30  19  0   0   0   0   0   0   0   0   0.000000    0
1   2   Col     1   31  32  33  22  0   0   0   0   0   0   0   0   0.000000    0
2   3   Col     1   34  35  36  25  0   0   0   0   0   0   0   0   0.000000    0
3   4   Col     2   19  20  21  10  0   0   0   0   0   0   0   0   0.000000    0
4   5   Col     2   22  23  24  13  0   0   0   0   0   0   0   0   0.000000    0
5   6   Col     2   25  26  27  16  0   0   0   0   0   0   0   0   0.000000    0
6   7   Col     3   10  11  12  1   0   0   0   0   0   0   0   0   0.000000    0
7   8   Col     3   13  14  15  4   0   0   0   0   0   0   0   0   0.000000    0
8   9   Col     3   16  17  18  7   0   0   0   0   0   0   0   0   0.000000    0
9   10  Row     1   19  20  21  22  -3660   -1500   -6792   0   0   0   0   0   3777.512143     0
10  11  Row     1   22  23  24  25  -3660   -1500   -6792   0   0   0   0   0   0.000000    0
11  12  Row     2   10  11  12  13  -3660   -1500   -6792   0   0   0   0   0   7555.024286     0
12  13  Row     2   13  14  15  16  -3660   -1500   -6792   0   0   0   0   0   0.000000    0
13  14  Row     3   1   2   3   4   -3660   -1500   -6792   0   0   0   0   0   11332.536429    0
14  15  Row     3   4   5   6   7   -3660   -1500   -6792   0   0   0   0   0   0.000000    0

To print without the row and column headers, use:

df = pd.DataFrame(poster)
print(df.to_string(index=False , header=False))

Output:

  1  Col  1  28  29  30  19     0     0     0  0  0  0  0  0      0.000000  0
  2  Col  1  31  32  33  22     0     0     0  0  0  0  0  0      0.000000  0
  3  Col  1  34  35  36  25     0     0     0  0  0  0  0  0      0.000000  0
  4  Col  2  19  20  21  10     0     0     0  0  0  0  0  0      0.000000  0

etc.

Another way to do this is to use print(repr(poster[i][j].rjust()) I would encourage you to read about it at this link to the Python docs

This implementation will only be printing the array (poster) to your terminal. To turn it into new_array with the correct justification, just replace the print statement.

First you need to make a copy of the array poster and transpose it: (This will enable you to find the max length of each position of the array)

transpose = list(zip(*poster))

Then, since there is a string ('Col', 'Row') in your data, you will need to account for it when finding the max length at each position: (Usig max(lst) won't help because one of your values is a string)

def find_abs_max_value(lst):
    max_value = max(lst)
    try:
        min_value = abs(min(lst))
    except TypeError:
        return max_value

    if max_value > min_value:
        return max_value
    else:
        return min_value

Then you can just do the following, and get your array: Here you go:

for i in range(len(poster)):
    for j in range(len(poster[i])):
        print(repr(poster[i][j]).rjust(len(str(find_abs_max_value(tranpose[j]))) + 1), end=', ')
    print('\n')

What you see above, it an implementation of repr(lst).rjust(some_length). What 'rjust' does is to right-justify each data in your arrray. Hope this was useful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM