Ask Your Question
3

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

asked 2017-05-01 02:21:50 +0100

cybervigilante gravatar image

updated 2023-01-09 23:59:43 +0100

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

Comments

This question was asked again a year later:

slelievre gravatar imageslelievre ( 2018-06-06 07:17:48 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-05-02 19:18:43 +0100

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
1

answered 2017-05-10 08:43:33 +0100

tmonteil gravatar image

updated 2017-05-10 09:04:22 +0100

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-05-01 02:21:50 +0100

Seen: 667 times

Last updated: May 10 '17