Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.set_string_function

numpy.set_string_function(f,repr=True)[source]

Set a Python function to be used when pretty printing arrays.

Parameters:

f : function or None

Function to be used to pretty print arrays. The function should expecta single array argument and return a string of the representation ofthe array. If None, the function is reset to the default NumPy functionto print arrays.

repr : bool, optional

If True (default), the function for pretty printing (__repr__)is set, if False the function that returns the default stringrepresentation (__str__) is set.

Examples

>>>defpprint(arr):...return'HA! - What are you going to do now?'...>>>np.set_string_function(pprint)>>>a=np.arange(10)>>>aHA! - What are you going to do now?>>>print(a)[0 1 2 3 4 5 6 7 8 9]

We can reset the function to the default:

>>>np.set_string_function(None)>>>aarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

repr affects either pretty printing or normal string representation.Note that__repr__ is still affected by setting__str__because the width of each array element in the returned string becomesequal to the length of the result of__str__().

>>>x=np.arange(4)>>>np.set_string_function(lambdax:'random',repr=False)>>>x.__str__()'random'>>>x.__repr__()'array([     0,      1,      2,      3])'

Previous topic

numpy.get_printoptions

Next topic

numpy.base_repr

  • © Copyright 2008-2009, The Scipy community.
  • Last updated on Jun 10, 2017.
  • Created usingSphinx 1.5.3.

[8]ページ先頭

©2009-2025 Movatter.jp