print array in a form that can then be input
I find it annoying that in casual use, if I print an array, that form can't be directly used as subsequent input (or can it?). What do others do about this? When I say casual, what I mean is, I write some long-running task and at the end, print some small array. Now I decide I'd like to cut/paste that into a new program.
For the case of a small array, you can use repr(). This will work as long as the array is not clipped (it is small enough).
a=np.arange(10) print a [0 1 2 3 4 5 6 7 8 9] repr(a) 'array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])'
a=np.arange(10000) a.resize((100,100)) print a [[ 0 1 2 ..., 97 98 99] [ 100 101 102 ..., 197 198 199] [ 200 201 202 ..., 297 298 299] ..., [9700 9701 9702 ..., 9797 9798 9799] [9800 9801 9802 ..., 9897 9898 9899] [9900 9901 9902 ..., 9997 9998 9999]] repr(a) 'array([[ 0, 1, 2, ..., 97, 98, 99],\n [ 100, 101, 102, ..., 197, 198, 199],\n [ 200, 201, 202, ..., 297, 298, 299],\n ..., \n [9700, 9701, 9702, ..., 9797, 9798, 9799],\n [9800, 9801, 9802, ..., 9897, 9898, 9899],\n [9900, 9901, 9902, ..., 9997, 9998, 9999]])'
David.
On Sat, Oct 20, 2012 at 1:11 PM, Neal Becker
I find it annoying that in casual use, if I print an array, that form can't be directly used as subsequent input (or can it?).
What do others do about this? When I say casual, what I mean is, I write some long-running task and at the end, print some small array. Now I decide I'd like to cut/paste that into a new program.
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Sat, Oct 20, 2012 at 4:40 AM, Daπid
a=np.arange(10) print a [0 1 2 3 4 5 6 7 8 9] repr(a) 'array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])'
Note that you don't need to explicitly call repr() at the interactive prompt: by default, Python prints the repr of an object when you type its name: In [24]: a=np.arange(10) In [25]: repr(a) Out[25]: 'array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])' In [26]: a Out[26]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) (this is ipython, but the behavior is the same in plain python). Cheers, f
On Sat, Oct 20, 2012 at 10:05 PM, Fernando Perez
Note that you don't need to explicitly call repr() at the interactive prompt: by default, Python prints the repr of an object when you type its name:
True, and good point to notice. Nevertheless print executes str(), if present, and it is the output of any non-interactive script. Just to complete your clarification. :)
participants (3)
-
Daπid
-
Fernando Perez
-
Neal Becker