numpy.
set_string_function
(f,repr = True )[源代码] ¶设置在漂亮打印数组时要使用的Python函数。
用于漂亮打印数组的函数。该函数应该期望一个数组参数,并返回该数组表示形式的字符串。如果为None,则该函数将重置为默认的NumPy函数以打印阵列。
如果为True(默认),__repr__
则设置漂亮打印功能();如果为False,则设置为返回默认字符串表示形式的功能(__str__
)。
也可以看看
例子
>>> def pprint(arr):
... return 'HA! - What are you going to do now?'
...
>>> np.set_string_function(pprint)
>>> a = np.arange(10)
>>> a
HA! - What are you going to do now?
>>> _ = a
>>> # [0 1 2 3 4 5 6 7 8 9]
我们可以将功能重置为默认值:
>>> np.set_string_function(None)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
repr影响漂亮的打印或正常的字符串表示形式。请注意,该__repr__
设置仍会受到影响,__str__
因为返回的字符串中每个数组元素的宽度等于的结果长度__str__()
。
>>> x = np.arange(4)
>>> np.set_string_function(lambda x:'random', repr=False)
>>> x.__str__()
'random'
>>> x.__repr__()
'array([0, 1, 2, 3])'