1

I have a problem that in the output of my code;
elements of each column does not place exactly beneath each other.


My original code is too busy, so I reduce it to a simple one;
so at first les's explain this simple one:


At first consider one simple question as follows: Write a code which recieves a natural number r, as number of rows;
and recieves another natural number c, as number of columns;
and then print all natural numbers form 1 to rc in r rows and c columns.


So the code will be something like the following:

r = int(input("How many Rows? "));     ## here r stands for number of rows 
c = int(input("How many columns? "));  ## here c stands for number of columns 

for i in range(1,r+1): 
    for j in range (1,c+1): 
        print(j+c*(i-1)) , 
    print 

and the output is as follows:

How many Rows? 5
How many columns? 6
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
>>> 

or:

How many Rows? 7
How many columns? 3
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
>>> 

What should I do, to get an output like this?

How many Rows? 5
How many columns? 6
 1  2  3  4  5  6
 7  8  9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
>>> 

or

How many Rows? 7
How many columns? 3
 1  2  3
 4  5  6
 7  8  9
10 11 12
13 14 15
16 17 18
19 20 21
>>> 



Now my original code is somthing like the following:

def function(n): 
    R=0;
    something...something...something...
    something...something...something...
    something...something...something...
    something...something...something...
    return(R)


r = int(input("How many Rows? "));     ## here r stands for number of rows 
c = int(input("How many columns? "));  ## here c stands for number of columns 

for i in range(0,r+1):
    for j in range(0,c+1)
        n=j+c*(i-1);
        r=function(n);
print (r)

Now for simplicity, suppose that by some by-hand-manipulation we get:
f(1)=function(1)=17, f(2)=235, f(3)=-8;
f(4)=-9641, f(5)=54278249, f(6)=411;

Now when I run the code the out put is as follows:

How many Rows? 2
How many columns? 3
17
235
-8
-9641
54278249 
41
>>> 

What shold I do to get an output like this:

How many Rows? 2
How many columns? 3
   17      235  -8
-9641 54278249 411
>>> 

Also note that I did not want to get something like this:

How many Rows? 2
How many columns? 3
17 235 -8
-9641 54278249 411
>>> 
Davood
  • 141
  • 2
  • 7

4 Answers4

3

Use rjust method:

r,c = 5,5

for i in range(1,r+1): 
    for j in range (1,c+1):
        str_to_printout = str(j+c*(i-1)).rjust(2) 
        print(str_to_printout), 
    print 

Result:

 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

UPD.

As for your last example, let's say f(n) is defined in this way:

def f(n):
    my_dict = {1:17, 2:235, 3:-8, 4:-9641, 5:54278249, 6:411}
    return my_dict.get(n, 0)

Then you can use the following approach:

r,c = 2,3

# data table with elemets in string format
data_str = [[str(f(j+c*(i-1))) for j in range (1,c+1)] for i in range(1,r+1)]

# transposed data table and list of max len for every column in data_str
data_str_transposed = [list(i) for i in zip(*data_str)] 
max_len_columns = [max(map(len, col)) for col in data_str_transposed] 

# printing out
# the string " " before 'join' is a delimiter between columns 
for row in data_str:
    print(" ".join(elem.rjust(max_len) for elem, max_len in zip(row, max_len_columns)))

Result:

   17      235  -8
-9641 54278249 411

With r,c = 3,3:

   17      235  -8
-9641 54278249 411
    0        0   0

Note that the indent in each column corresponds to the maximum length in this column, and not in the entire table.

Community
  • 1
  • 1
MaximTitarenko
  • 886
  • 4
  • 8
1

Hope this helps. Please comment if you need any further clarifications.

#   result stores the final matrix
#   max_len stores the length of maximum element

result, max_len = [], 0
for i in range(1, r + 1):
    temp = []
    for j in range(1, c + 1):
        n = j + c * (i - 1);
        r = function(n);
        if len(str(r)) > max_len:
            max_len = len(str(r))
        temp.append(r)
    result.append(temp)

#   printing the values seperately to apply rjust() to each and every element

for i in result:
    for j in i:
        print(str(j).rjust(max_len), end=' ')
    print()
Deepayan Ghosh
  • 185
  • 2
  • 9
1

Adopted from MaximTitarenko's answer:

You first look for the minimum and maximum value, then decide which is the longer one and use its length as the value for the rjust(x) call.

import random 

r,c = 15,5

m = random.sample(xrange(10000), 100)
length1 = len(str(max(m)))
length2 = len(str(min(m)))

longest = max(length1, length2)

for i in range(r): 
    for j in range (c):
        str_to_printout = str(m[i*c+j]).rjust(longest) 
        print(str_to_printout),
    print

Example output:

 937 9992 8602 4213 7053
1957 9766 6704 8051 8636
 267  889 1903 8693 5565
8287 7842 6933 2111 9689
3948  428 8894 7522  417
3708 8033  878 4945 2771
6393   35 9065 2193 6797
5430 2720  647 4582 3316
9803 1033 7864  656 4556
6751 6342 4915 5986 6805
9490 2325 5237 8513 8860
8400 1789 2004 4500 2836
8329 4322 6616  132 7198
4715  193 2931 3947 8288
1338 9386 5036 4297 2903
voiDnyx
  • 975
  • 1
  • 11
  • 24
0

You need to use the string method .rjust

From the documentation (linked above):

string.rjust(s, width[, fillchar])

This function right-justifies a string in a field of given width. It returns a string that is at least width characters wide, created by padding the string with the character fillchar (default is a space) until the given width on the right. The string is never truncated.

So we need to calculate what the width (in characters) each number should be padded to. That is pretty simple, just the number of rows * number of columns + 1 (the +1 adds a one-space gab between each column).

Using this, it becomes quite simple to write the code:

r = int(input("How many Rows? "))
c = int(input("How many columns? "))

width = len(str(r*c)) + 1
for i in range(1,r+1): 
    for j in range(1,c+1): 
        print str(j+c*(i-1)).rjust(width) ,
    print

which for an r, c of 4, 5 respectively, outputs:

  1   2   3   4   5
  6   7   8   9  10
 11  12  13  14  15
 16  17  18  19  20

Hopefully this helps you out and you can adapt this to other situations yourself!

Community
  • 1
  • 1
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54