13

I have a 2-dimensional numpy array that looks like this:

[[a b c]
 [d e f]
 [g h i]]

I'd like to print it without any of the default notational fluff that typically comes with arrays; namely the [, ] and the spaces between the elements. Something like this:

abc
def
ghi

Is it possible to do such a thing (without a trivial and possibly expensive Python loop, of course)?

I have looked at numpy.set_printoptions but it looks like it only sets presentational options for how elements are displayed, not the characters in between.

Edit: The elements in the array have a string representation that can be anything, including [, ] and whitespace. Minimal example of how to build such an array:

class custom(object):
    def __repr__(self):
        return 'a'
a = numpy.empty((5, 5), custom)
a.fill(custom())
print a
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Etienne Perot
  • 4,764
  • 7
  • 40
  • 50
  • Just to be clear: your custom repr includes a terminal `.` that you want to get rid of, but might include others that you don't? I'm not sure I understand what you want to strip. – DSM Mar 22 '12 at 20:18
  • My bad, this was due to me editing the representation by hand. My original question used a numpy array of regular numbers, for which there is a `.` after each number used as delimiter (or as decimal separator, not sure). Later, I edited my question to reflect that the data type of the array was not necessarily numerical, and forgot to change the delimiter accordingly. Edited again. – Etienne Perot Mar 22 '12 at 20:28
  • Subset: only remove `[]`: http://stackoverflow.com/questions/9360103/how-to-print-a-numpy-array-without-brackets/42046765#42046765 – Ciro Santilli OurBigBook.com Feb 04 '17 at 23:18

4 Answers4

10

While this pretty much amounts to a loop, I think this is probably the best you're going to get. Generally the join string method is pretty fast.

>>> a = np.array([[1,2,3],[2,4,6],[-1,-2,-3]])
>>> print '\n'.join(''.join(str(cell) for cell in row) for row in a)
123
246
-1-2-3

I think at this point you'd probably be best off implementing something and measuring how long it takes. My guess is that the slowest part of the code will be actually printing to console, not joining the strings together.

Wilduck
  • 13,822
  • 10
  • 58
  • 90
8
np.savetxt(sys.stdout.buffer, a, fmt='%s', delimiter='')
panda-34
  • 4,089
  • 20
  • 25
1
>>> import numpy    
>>> a = numpy.array([[ 1.0,  2,  3], [ 4,  5,  6], [ 7,  8,  9]])
>>> print str(a).replace(' ','').replace('.','').replace('[','').replace(']','')
123
456
789
fraxel
  • 34,470
  • 11
  • 98
  • 102
  • +1 because it does answer the question I asked, but the real array in question uses a custom data type for which the string representation can be anything, including spaces and brackets. Edited the question to reflect this – Etienne Perot Mar 22 '12 at 20:11
  • @Etienne Perot - back to the drawing board! – fraxel Mar 22 '12 at 20:22
0

It's pretty simple, if a is your array just do this:

print(re.sub('[ ]+', ' ', re.sub(' *[\\[\\]] *', '', np.array_str(a))))
arco444
  • 22,002
  • 12
  • 63
  • 67