0

I'm currently practicing how to use the zip() function to do clean print outs etc.

It works kind of good, but still, I'm getting these weird unintended indentations in my out-prints.

My code goes like this:

data_list1 = [[7.2, 8.1, 8.9, 11.6, 7.7, 8.7, 6.9], [5.4, 8.8, 8.9, 3.7, 3.3, 5.2, 9.6], [10.8, 5.0, 5.4, 9.5, 5.3, 5.8, 2.3], [4.1, 6.6, 8.2, 6.1, 8.9, 6.6, 4.1], [2.8, 2.1, 4.1]]
data_list2 = [[9.8, 11.6, 11.5, 13.3, 12.6, 10.3, 7.5], [9.3, 10.3, 10.3, 8.4, 8.8, 5.0, 5.8], [6.8, 2.3, 3.5, 7.9, 11.8, 10.7, 9.0], [5.8, 6.8, 11.7, 10.6, 11.7, 13.1, 13.6], [8.0, 3.5, 3.2]]


def clean_up(list1, list2):

    data_list1 = []
    for i in list1:
        for j in i:
            data_list1.append(j)

    data_list2 = []
    for i in list2:
        for j in i:
            data_list2.append(j)

    for data_list1, data_list2 in zip(data_list1, data_list2):
        print(f"{data_list1}\t\t\t{data_list2}")


print(clean_up(data_list1,data_list2))

And my out-print is:

7.2         9.8
8.1         11.6
8.9         11.5
11.6            13.3
7.7         12.6
8.7         10.3
6.9         7.5
5.4         9.3
8.8         10.3
8.9         10.3
3.7         8.4
3.3         8.8
5.2         5.0
9.6         5.8
10.8            6.8
5.0         2.3
5.4         3.5
9.5         7.9
5.3         11.8
5.8         10.7
2.3         9.0
4.1         5.8
6.6         6.8
8.2         11.7
6.1         10.6
8.9         11.7
6.6         13.1
4.1         13.6
2.8         8.0
2.1         3.5
4.1         3.2

As you might see, there are a few spots where the out-print look's kind of strange.

My two questions are:

  1. Is there anyone who has some tips on how to fix this?
  2. If so, would this technique also make a similar look if I write the data onto a file as well?

All help is welcomed.

  • Read up on "field width" (in the string format specifier). – Jussi Nurminen Sep 27 '21 at 15:00
  • 1
    Does this answer your question? [How can I fill out a Python string with spaces?](https://stackoverflow.com/questions/5676646/how-can-i-fill-out-a-python-string-with-spaces). you can use string padding – sahasrara62 Sep 27 '21 at 15:11
  • @sahasrara62: yeah, it kind of does. I solved the problem by using ```print(f"{data_list1:<10}{data_list2}")``` insted :-) – fredericoamigo Sep 27 '21 at 15:15

2 Answers2

1

To avoid the occasional misalignment when using tab separators, all of the values in each column should be the same length. Otherwise you'll sometimes need more (or fewer) tabs to keep things aligned depending on the printed width of each value. With constant width values, you won't actually need tab separators but it might still be required if you are writing the data to a file and want to extract it back later.

Here's a function that will return the adjusted lines to print or save a 2D matrix. Because you want the output to be formatted with all values aligned, you'll need to combine the sublists into a single 2D matrix in order to take all value widths into acount.

def matrixLines(M,sep="\t"):
     widths = [max(map(len,map(str,col))) for col in zip(*M)]  # column widths
     return [sep.join(f"{v:<{w}}" for v,w in zip(line,widths)) # format & join
             for line in M]

Setup:

# combine into a single 2D matrix:
M = [[*ln] for dl in zip(data_list1,data_list2) for ln in zip(*dl)]

Output:

print(*matrixLines(M),sep="\n")
7.2   9.8 
8.1   11.6
8.9   11.5
11.6  13.3
7.7   12.6
8.7   10.3
6.9   7.5 
5.4   9.3 
8.8   10.3
8.9   10.3
3.7   8.4 
3.3   8.8 
5.2   5.0 
9.6   5.8 
10.8  6.8 
5.0   2.3 
5.4   3.5 
9.5   7.9 
5.3   11.8
5.8   10.7
2.3   9.0 
4.1   5.8 
6.6   6.8 
8.2   11.7
6.1   10.6
8.9   11.7
6.6   13.1
4.1   13.6
2.8   8.0 
2.1   3.5 
4.1   3.2

Note that the matrixLines function is not limited to two columns. It will work for any rectangular matrix (list of lists):

print(*matrixLines(data_list1[:4]),sep="\n")

7.2   8.1  8.9  11.6  7.7  8.7  6.9
5.4   8.8  8.9  3.7   3.3  5.2  9.6
10.8  5.0  5.4  9.5   5.3  5.8  2.3
4.1   6.6  8.2  6.1   8.9  6.6  4.1
Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

If someone in the future might have the same question, I solved the problem by replacing the \t\t\t (new-tab) with print(f"{data_list1:<10}{data_list2}")