如何打印Numpy数组没有任何额外的表示法(方括号[]和元素之间的空格)?

Eti*_*rot 9 python arrays formatting numpy

我有一个二维numpy数组,看起来像这样:

[[a b c]
 [d e f]
 [g h i]]
Run Code Online (Sandbox Code Playgroud)

我想打印它没有通常带有阵列的任何默认的符号绒毛; 即[,]和元素之间的空间.像这样的东西:

abc
def
ghi
Run Code Online (Sandbox Code Playgroud)

是否有可能做这样的事情(当然没有一个微不足道的,可能是昂贵的Python循环)?

我看过numpy.set_printoptions但看起来它只设置元素显示方式的表示选项,而不是两者之间的字符.

编辑:阵列中的元件具有字符串表示,可以是任何东西,包括[,]和空白.如何构建这样一个数组的最小例子:

class custom(object):
    def __repr__(self):
        return 'a'
a = numpy.empty((5, 5), custom)
a.fill(custom())
print a
Run Code Online (Sandbox Code Playgroud)

Wil*_*uck 9

虽然这几乎相当于一个循环,但我认为这可能是你最好的.通常join字符串方法非常快.

>>> 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
Run Code Online (Sandbox Code Playgroud)

我认为在这一点上你可能最好实现一些东西并测量它需要多长时间.我的猜测是,代码中最慢的部分实际上是打印到控制台,而不是将字符串连接在一起.


pan*_*-34 8

np.savetxt(sys.stdout.buffer, a, fmt='%s', delimiter='')
Run Code Online (Sandbox Code Playgroud)