Ask Your Question
2

If I return a list from a function that is more than 26 elements, the list goes vertical

asked 2017-04-30 19:21:50 -0500

cybervigilante gravatar image

updated 2017-05-10 01:44:07 -0500

tmonteil gravatar image

If I return a list from a function that is more than 26 elements, the list goes vertical instead of horizontal even if I turn pretty printing off. Is this normal, and can I turn it off? I'm just returning the list, not using print explicitly. i.e.

With range 26 in the testfunc it shows a horizontal list, but if I increase that to 27 it goes vertical:

def tester():
    v=[]
    for i in range(26):
        v.append(3)
    return v
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-05-02 12:18:43 -0500

dan_fulea gravatar image

sage does it for us. (And i really enjoy the output while printing e.g. some points on elliptic curves.)

But often space is expensive. In such cases i use my own explicit will (after seeing the mess..). For instance, from the sage interpreter opened in a linux console, here are some more or less equivalent ways:

sage: v = 30 * [3,]    # comma is not needed, but humanly clears, this is a list repeated 30x
sage: # v = [ 3 for _ in range(30) ]    # would be the list comprehension for the above
sage: repr(v)
'[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]'
sage: print repr(v)
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
sage: str(v)
'[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]'
sage: print str(v)
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
sage: print "v = %s" % v
v = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
sage: v.__str__()
'[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]'
sage: v.__repr__()
'[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]'
sage: v

[3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3, 
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3]

(The last is the mess, that we often want to avoid on the small laptop terminal in the train.)

edit flag offensive delete link more
0

answered 2017-05-10 01:43:33 -0500

tmonteil gravatar image

updated 2017-05-10 02:04:22 -0500

There is a "pretty print" feature of ipython/jupyter, that can be switched off with the %pprint magic:

In [2]: tester()
Out[2]: 
[3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3,
 3]

In [3]: %pprint
Pretty printing has been turned OFF

In [4]: tester()
Out[4]: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

However, this was from a genuine ipython/jupyter console (loaunched from Sage with sage -ipython), not Sage's one, for which it does not work, so there might be a broken interaction with Sage's own pretty printing features.

This is now trac ticket 22968, thanks for reporting.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-04-30 19:21:50 -0500

Seen: 60 times

Last updated: May 10