0

I have a list of numbers which are the output data of an OCR operation. There are 40 intergers, I want to print them in the form of a matrix(8x5). Can any one please help me how to do this in Python 2.7? I dont want to enter the elements manually.. the list of elements are being generated using for loops, i just want to display them in the form of 8x5 matrix.

thank you

3 Answers3

2

Simply use list comprehension and range() function.

my_list = [1, 2, 3, ..., 40]
array = [[my_list[j*5 + i] for i in range(5)] for j in range(8)]

You can then use any function to display it as a matrix:

for row in array:
    print(row)

If you need the matrix to be "nicely" displayed, you can use HappyLeapSecond's solution:

print('\n'.join([''.join(['{:4}'.format(item) for item in row]) 
      for row in array]))

See example: https://ideone.com/yOk1I5

Community
  • 1
  • 1
Delgan
  • 18,571
  • 11
  • 90
  • 141
  • Thanks for your reply, but I dont want to enter the elements manually, my elements are generated form this function - pytesseract.image_to_string(im), i want to store the result of this in one array..and then print the same. – yash Wanth Shetty Jul 05 '15 at 18:21
  • @yashWanthShetty `image_to_string()` is returning a string, right? So you have to convert it to a list just using `my_list = list(pytesseract.image_to_string(im))`. If the string contains 40 chars, then you can use the code I provided to convert it to a 8x5 matrix. – Delgan Jul 05 '15 at 18:42
  • This almost worked ,but i'm able to display only the last element...everything else is overwritten and 'my_list' contains only the last element.. How to solve this? – yash Wanth Shetty Jul 05 '15 at 19:36
  • @yashWanthShetty Do you call `image_to_string()` several times, inside a loop for example? I can not really help you without seeing your code. – Delgan Jul 05 '15 at 19:46
  • `i=0 for a in range (1,9): for b in range(1,6): ball = th1[145*(a-1):145*a, (b-1)*171:171*b] cv.imwrite('OpImage{:>40}.png'.format(i),ball) im = Image.open("OpImage{:>40}.png".format(i)) print pytesseract.image_to_string(im) ` #I have to store all the values generated here and print them in the form of matrix – yash Wanth Shetty Jul 05 '15 at 20:28
  • I'm not able to put my code properly in the commentbox, if you dont understand the code..please post your mail id.. i will revert to you with my proper code.. thank you – yash Wanth Shetty Jul 05 '15 at 20:34
  • @yashWanthShetty You can host you code on [Pastebin](http://pastebin.com/) ans share the link with me. – Delgan Jul 05 '15 at 20:39
  • [link](http://pastebin.com/wtwdHppW) I have pasted my code here – yash Wanth Shetty Jul 05 '15 at 21:02
  • @yashWanthShetty Does it fit your needs: http://pastebin.com/3LCDadZj ? – Delgan Jul 05 '15 at 21:09
  • I used your format and tried printing ..it worked! but when i try to print them in matrix form it is showing the error `array = [[p[j*5 + i] for i in range(5)] for j in range(8)] IndexError: string index out of range` I've pasted my complete code check it out http://pastebin.com/8CRkVkmv I even tried to print inside the for loop itself..but that showed same error. – yash Wanth Shetty Jul 05 '15 at 21:41
  • @yashWanthShetty You probably need to use `values` instead of `p`: `array = [[values[j*5 + i] for i in range(5)] for j in range(8)]`. The variable `values` contains actually the 40 integers you want to store in a matrix. – Delgan Jul 05 '15 at 21:48
  • You Nailed it! Thanks a lot! I got exactly what I wanted:) – yash Wanth Shetty Jul 05 '15 at 21:52
0

Another solution:

def print_matrix(numbers, n):
    res = ''
    for i in range(len(numbers)):
        res += '{:2} '.format(numbers[i])
        if (i + 1) % n == 0:
            res += '\n'
    print(res)

Output:

>>> print_matrix([i for i in range(40)], 5)
 0  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 31 32 33 34
35 36 37 38 39
clemtoy
  • 1,681
  • 2
  • 18
  • 30
0

Welcome to stackoverflow! The following would work for an 8x5 display:

import random, itertools

#  Create a list of 40 random integers    
l_ocr = random.sample(xrange(1024), 8*5)

# Read 8 integers out of the list at a time
for row in itertools.izip(*([iter(l_ocr)] * 8)):
    # For each integer in the row, print it right aligned
    for col in row:
        print "{:>6d} ".format(col),
    print   # Newline after each row

Giving:

   325     631     967     289     700     754     602     550 
   641      55     476     805     442     964     412     823 
   621     559     276     333     903     956     206     875 
   630     138     732     487     930     254     464     161 
   422     201     723     353     853     147     523     510
Martin Evans
  • 45,791
  • 17
  • 81
  • 97