I have a 2-dimensional numpy array that looks like this:
我有一个二维numpy数组,如下所示:
[[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)?
是否可以做这样的事情(当然没有一个微不足道的,可能是昂贵的Python循环)?
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.
我看过numpy.set_printoptions但看起来它只设置元素显示方式的表示选项,而不是两者之间的字符。
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
4 个解决方案
#1
8
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.
我认为在这一点上你可能最擅长实现某些东西并测量它需要多长时间。我的猜测是,代码中最慢的部分实际上是打印到控制台,而不是将字符串连接在一起。
#2
3
np.savetxt(sys.stdout.buffer, a, fmt='%s', delimiter='')
#3
0
>>> 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
#4
0
It's pretty simple, if a
is your array just do this:
这很简单,如果a是你的数组就这样做:
print(re.sub('[ ]+', ' ', re.sub(' *[\\[\\]] *', '', np.array_str(a))))
#1
8
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.
我认为在这一点上你可能最擅长实现某些东西并测量它需要多长时间。我的猜测是,代码中最慢的部分实际上是打印到控制台,而不是将字符串连接在一起。
#2
3
np.savetxt(sys.stdout.buffer, a, fmt='%s', delimiter='')
#3
0
>>> 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
#4
0
It's pretty simple, if a
is your array just do this:
这很简单,如果a是你的数组就这样做:
print(re.sub('[ ]+', ' ', re.sub(' *[\\[\\]] *', '', np.array_str(a))))