ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 02 Nov 2017 10:18:10 +0100how to print the printout not too long on Sagecellhttps://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/I try to printout this.
myList = []
for i in range (64):
myList.append(i)
print myList
Result:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
What i need is like below
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
Someone help me pls...Sat, 21 Oct 2017 17:01:06 +0200https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/Comment by dan_fulea for <p>I try to printout this.</p>
<pre><code>myList = []
for i in range (64):
myList.append(i)
print myList
</code></pre>
<p>Result:</p>
<pre><code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
</code></pre>
<p>What i need is like below</p>
<pre><code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
</code></pre>
<p>Someone help me pls...</p>
https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39247#post-id-39247`myList = range(64)` is enough to define that list.Sun, 22 Oct 2017 18:15:09 +0200https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39247#post-id-39247Answer by calc314 for <p>I try to printout this.</p>
<pre><code>myList = []
for i in range (64):
myList.append(i)
print myList
</code></pre>
<p>Result:</p>
<pre><code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
</code></pre>
<p>What i need is like below</p>
<pre><code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
</code></pre>
<p>Someone help me pls...</p>
https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?answer=39241#post-id-39241How about:
num_rows=4
for i in range(num_rows):
print [myList[i*64/num_rows+j] for j in range(64/num_rows)]
Sun, 22 Oct 2017 05:12:28 +0200https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?answer=39241#post-id-39241Comment by Niyamabrata for <p>How about:</p>
<pre><code>num_rows=4
for i in range(num_rows):
print [myList[i*64/num_rows+j] for j in range(64/num_rows)]
</code></pre>
https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39242#post-id-39242what about the number of list is odd?
I have tried this below, but still not work.
myList = []
for i in range (67):
myList.append(i)
L = len(myList)
num_rows=L//8
for i in range(num_rows):
print [myList[i*L//num_rows+j] for j in range(L//num_rows)]Sun, 22 Oct 2017 06:13:55 +0200https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39242#post-id-39242Comment by dan_fulea for <p>How about:</p>
<pre><code>num_rows=4
for i in range(num_rows):
print [myList[i*64/num_rows+j] for j in range(64/num_rows)]
</code></pre>
https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39248#post-id-39248This is a completely new question... (a more general one.)Sun, 22 Oct 2017 18:15:54 +0200https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39248#post-id-39248Answer by dan_fulea for <p>I try to printout this.</p>
<pre><code>myList = []
for i in range (64):
myList.append(i)
print myList
</code></pre>
<p>Result:</p>
<pre><code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
</code></pre>
<p>What i need is like below</p>
<pre><code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
</code></pre>
<p>Someone help me pls...</p>
https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?answer=39249#post-id-39249Let us then make the question more specific, if the other answer is not fully satisfactory.
So we have a list, and the problem is to display it so that it fits in some shape. Well, this fit is hard to determine, so we want that at most $16$ items of the list are printed on each line, the elements of the list being taken one by one, if there is a new set of $16$ elements remained in the list, then we display them all, else only the remaining ones.
**But**: This must be done dynamically with respect to some variable, which specialized to $16$ delivers the above.
And since we do not return a list, since it cannot capture this display information, we return a string.
(Since only a string can capture the newlines at the right points.)
OK, let us type some code:
def customStingForList( myList, nMostInARow ):
if not myList:
return myList
s = '['
N = len(myList)
for k in range(N):
s += '%s' % myList[k]
if k+1 == N:
s += ']'
break
if (k+1) % nMostInARow == 0:
s += ',\n'
else:
s += ', '
return s
Let us test the function:
sage: customStingForList( [0..10], 6 )
'[0, 1, 2, 3, 4, 5,\n6, 7, 8, 9, 10]'
sage: print customStingForList( [0..10], 6 )
[0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10]
So the result is a sting, and only after we print that sting we get the rearrangement of the too long list. Now for the (fragmented and assambled) posted list `[0..63]` or `range(64)`...
sage: print customStingForList( [0..63], 16 )
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
sage: print customStingForList( [0..63], 11 )
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62, 63]
Alternative solution:
def customStingForList2( myList, nMostInARow ):
N = len( myList )
blocks = [ myList[ k*nMostInARow : min(N, (k+1)*nMostInARow) ]
for k in range( 1 + N // nMostInARow ) ]
strBlocks = [ ', '.join( [ str(entry) for entry in block ] )
for block in blocks
if block]
return '[%s]' % ( ',\n'.join( strBlocks ) )
Let us test it:
sage: myList = range(64)
sage: print customStingForList2( myList, 10 )
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63]
sage: print customStingForList2( myList, 16 )
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
Sun, 22 Oct 2017 18:16:00 +0200https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?answer=39249#post-id-39249Comment by Niyamabrata for <p>Let us then make the question more specific, if the other answer is not fully satisfactory. </p>
<p>So we have a list, and the problem is to display it so that it fits in some shape. Well, this fit is hard to determine, so we want that at most $16$ items of the list are printed on each line, the elements of the list being taken one by one, if there is a new set of $16$ elements remained in the list, then we display them all, else only the remaining ones.</p>
<p><strong>But</strong>: This must be done dynamically with respect to some variable, which specialized to $16$ delivers the above.</p>
<p>And since we do not return a list, since it cannot capture this display information, we return a string.
(Since only a string can capture the newlines at the right points.)</p>
<p>OK, let us type some code:</p>
<pre><code>def customStingForList( myList, nMostInARow ):
if not myList:
return myList
s = '['
N = len(myList)
for k in range(N):
s += '%s' % myList[k]
if k+1 == N:
s += ']'
break
if (k+1) % nMostInARow == 0:
s += ',\n'
else:
s += ', '
return s
</code></pre>
<p>Let us test the function:</p>
<pre><code>sage: customStingForList( [0..10], 6 )
'[0, 1, 2, 3, 4, 5,\n6, 7, 8, 9, 10]'
sage: print customStingForList( [0..10], 6 )
[0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10]
</code></pre>
<p>So the result is a sting, and only after we print that sting we get the rearrangement of the too long list. Now for the (fragmented and assambled) posted list <code>[0..63]</code> or <code>range(64)</code>...</p>
<pre><code>sage: print customStingForList( [0..63], 16 )
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
sage: print customStingForList( [0..63], 11 )
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62, 63]
</code></pre>
<p>Alternative solution:</p>
<pre><code>def customStingForList2( myList, nMostInARow ):
N = len( myList )
blocks = [ myList[ k*nMostInARow : min(N, (k+1)*nMostInARow) ]
for k in range( 1 + N // nMostInARow ) ]
strBlocks = [ ', '.join( [ str(entry) for entry in block ] )
for block in blocks
if block]
return '[%s]' % ( ',\n'.join( strBlocks ) )
</code></pre>
<p>Let us test it:</p>
<pre><code>sage: myList = range(64)
sage: print customStingForList2( myList, 10 )
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63]
sage: print customStingForList2( myList, 16 )
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
</code></pre>
https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39254#post-id-39254Thanks you so much...i like the second one...its works nice on my list. God gives u the best..Mon, 23 Oct 2017 10:13:00 +0200https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39254#post-id-39254Answer by slelievre for <p>I try to printout this.</p>
<pre><code>myList = []
for i in range (64):
myList.append(i)
print myList
</code></pre>
<p>Result:</p>
<pre><code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
</code></pre>
<p>What i need is like below</p>
<pre><code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
</code></pre>
<p>Someone help me pls...</p>
https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?answer=39252#post-id-39252To solve this problem, you might want to write a custom print function.
The maximum line length can be an optional argument with a set default.
Below is one way to write such a custom print function that would apply
not only to lists but to any Sage object, taking advantage of the spaces
in its string representation to introduce line breaks.
The examples in the function's documentation show how to use it for a list
and for a polynomial expression, but it would work with any other object.
def flowed(a, flow=72):
"""
Print this Sage object keeping line length below the specified bound
This will introduce line breaks where the string representation
of this object contains spaces.
INPUT:
- ``a`` -- a Sage object to be printed
- ``flow`` (optional, default: 72) -- bound for line length
EXAMPLES::
sage: a = range(32)
sage: flowed(a, flow=32)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31]
sage: flowed(a, flow=48)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31]
sage: p = taylor(log(1 + x), x, 0, 20)
sage: flowed(p)
-1/20*x^20 + 1/19*x^19 - 1/18*x^18 + 1/17*x^17 - 1/16*x^16 + 1/15*x^15 -
1/14*x^14 + 1/13*x^13 - 1/12*x^12 + 1/11*x^11 - 1/10*x^10 + 1/9*x^9 -
1/8*x^8 + 1/7*x^7 - 1/6*x^6 + 1/5*x^5 - 1/4*x^4 + 1/3*x^3 - 1/2*x^2 + x
"""
blocks = str(a).split()
s = blocks.pop(0)
c = len(s) + 1
while blocks:
b = blocks.pop(0)
p = len(b)
if c + p > flow:
print s
s = ''
c = 1
elif s:
s += ' '
c += 1
s += b
c += p
print s
Note that the documentation of the function can be accessed by typing
sage: flowed?
Mon, 23 Oct 2017 05:34:04 +0200https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?answer=39252#post-id-39252Comment by Niyamabrata for <p>To solve this problem, you might want to write a custom print function.</p>
<p>The maximum line length can be an optional argument with a set default.</p>
<p>Below is one way to write such a custom print function that would apply
not only to lists but to any Sage object, taking advantage of the spaces
in its string representation to introduce line breaks.</p>
<p>The examples in the function's documentation show how to use it for a list
and for a polynomial expression, but it would work with any other object.</p>
<pre><code>def flowed(a, flow=72):
"""
Print this Sage object keeping line length below the specified bound
This will introduce line breaks where the string representation
of this object contains spaces.
INPUT:
- ``a`` -- a Sage object to be printed
- ``flow`` (optional, default: 72) -- bound for line length
EXAMPLES::
sage: a = range(32)
sage: flowed(a, flow=32)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31]
sage: flowed(a, flow=48)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31]
sage: p = taylor(log(1 + x), x, 0, 20)
sage: flowed(p)
-1/20*x^20 + 1/19*x^19 - 1/18*x^18 + 1/17*x^17 - 1/16*x^16 + 1/15*x^15 -
1/14*x^14 + 1/13*x^13 - 1/12*x^12 + 1/11*x^11 - 1/10*x^10 + 1/9*x^9 -
1/8*x^8 + 1/7*x^7 - 1/6*x^6 + 1/5*x^5 - 1/4*x^4 + 1/3*x^3 - 1/2*x^2 + x
"""
blocks = str(a).split()
s = blocks.pop(0)
c = len(s) + 1
while blocks:
b = blocks.pop(0)
p = len(b)
if c + p > flow:
print s
s = ''
c = 1
elif s:
s += ' '
c += 1
s += b
c += p
print s
</code></pre>
<p>Note that the documentation of the function can be accessed by typing</p>
<pre><code>sage: flowed?
</code></pre>
https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39255#post-id-39255i cant find flowed function on sagecell...Mon, 23 Oct 2017 10:13:37 +0200https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39255#post-id-39255Comment by slelievre for <p>To solve this problem, you might want to write a custom print function.</p>
<p>The maximum line length can be an optional argument with a set default.</p>
<p>Below is one way to write such a custom print function that would apply
not only to lists but to any Sage object, taking advantage of the spaces
in its string representation to introduce line breaks.</p>
<p>The examples in the function's documentation show how to use it for a list
and for a polynomial expression, but it would work with any other object.</p>
<pre><code>def flowed(a, flow=72):
"""
Print this Sage object keeping line length below the specified bound
This will introduce line breaks where the string representation
of this object contains spaces.
INPUT:
- ``a`` -- a Sage object to be printed
- ``flow`` (optional, default: 72) -- bound for line length
EXAMPLES::
sage: a = range(32)
sage: flowed(a, flow=32)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31]
sage: flowed(a, flow=48)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31]
sage: p = taylor(log(1 + x), x, 0, 20)
sage: flowed(p)
-1/20*x^20 + 1/19*x^19 - 1/18*x^18 + 1/17*x^17 - 1/16*x^16 + 1/15*x^15 -
1/14*x^14 + 1/13*x^13 - 1/12*x^12 + 1/11*x^11 - 1/10*x^10 + 1/9*x^9 -
1/8*x^8 + 1/7*x^7 - 1/6*x^6 + 1/5*x^5 - 1/4*x^4 + 1/3*x^3 - 1/2*x^2 + x
"""
blocks = str(a).split()
s = blocks.pop(0)
c = len(s) + 1
while blocks:
b = blocks.pop(0)
p = len(b)
if c + p > flow:
print s
s = ''
c = 1
elif s:
s += ' '
c += 1
s += b
c += p
print s
</code></pre>
<p>Note that the documentation of the function can be accessed by typing</p>
<pre><code>sage: flowed?
</code></pre>
https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39332#post-id-39332Hi, `flowed` is not a function provided by Sage. You need to define it as in my answer.
I was just commenting that if you include a documentation string as I suggested, you can
then inspect the function's documentation.Mon, 30 Oct 2017 02:13:20 +0100https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39332#post-id-39332Comment by Niyamabrata for <p>To solve this problem, you might want to write a custom print function.</p>
<p>The maximum line length can be an optional argument with a set default.</p>
<p>Below is one way to write such a custom print function that would apply
not only to lists but to any Sage object, taking advantage of the spaces
in its string representation to introduce line breaks.</p>
<p>The examples in the function's documentation show how to use it for a list
and for a polynomial expression, but it would work with any other object.</p>
<pre><code>def flowed(a, flow=72):
"""
Print this Sage object keeping line length below the specified bound
This will introduce line breaks where the string representation
of this object contains spaces.
INPUT:
- ``a`` -- a Sage object to be printed
- ``flow`` (optional, default: 72) -- bound for line length
EXAMPLES::
sage: a = range(32)
sage: flowed(a, flow=32)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31]
sage: flowed(a, flow=48)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31]
sage: p = taylor(log(1 + x), x, 0, 20)
sage: flowed(p)
-1/20*x^20 + 1/19*x^19 - 1/18*x^18 + 1/17*x^17 - 1/16*x^16 + 1/15*x^15 -
1/14*x^14 + 1/13*x^13 - 1/12*x^12 + 1/11*x^11 - 1/10*x^10 + 1/9*x^9 -
1/8*x^8 + 1/7*x^7 - 1/6*x^6 + 1/5*x^5 - 1/4*x^4 + 1/3*x^3 - 1/2*x^2 + x
"""
blocks = str(a).split()
s = blocks.pop(0)
c = len(s) + 1
while blocks:
b = blocks.pop(0)
p = len(b)
if c + p > flow:
print s
s = ''
c = 1
elif s:
s += ' '
c += 1
s += b
c += p
print s
</code></pre>
<p>Note that the documentation of the function can be accessed by typing</p>
<pre><code>sage: flowed?
</code></pre>
https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39371#post-id-39371Thank you so much...i'm new to this appsThu, 02 Nov 2017 10:18:10 +0100https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39371#post-id-39371Comment by slelievre for <p>To solve this problem, you might want to write a custom print function.</p>
<p>The maximum line length can be an optional argument with a set default.</p>
<p>Below is one way to write such a custom print function that would apply
not only to lists but to any Sage object, taking advantage of the spaces
in its string representation to introduce line breaks.</p>
<p>The examples in the function's documentation show how to use it for a list
and for a polynomial expression, but it would work with any other object.</p>
<pre><code>def flowed(a, flow=72):
"""
Print this Sage object keeping line length below the specified bound
This will introduce line breaks where the string representation
of this object contains spaces.
INPUT:
- ``a`` -- a Sage object to be printed
- ``flow`` (optional, default: 72) -- bound for line length
EXAMPLES::
sage: a = range(32)
sage: flowed(a, flow=32)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31]
sage: flowed(a, flow=48)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31]
sage: p = taylor(log(1 + x), x, 0, 20)
sage: flowed(p)
-1/20*x^20 + 1/19*x^19 - 1/18*x^18 + 1/17*x^17 - 1/16*x^16 + 1/15*x^15 -
1/14*x^14 + 1/13*x^13 - 1/12*x^12 + 1/11*x^11 - 1/10*x^10 + 1/9*x^9 -
1/8*x^8 + 1/7*x^7 - 1/6*x^6 + 1/5*x^5 - 1/4*x^4 + 1/3*x^3 - 1/2*x^2 + x
"""
blocks = str(a).split()
s = blocks.pop(0)
c = len(s) + 1
while blocks:
b = blocks.pop(0)
p = len(b)
if c + p > flow:
print s
s = ''
c = 1
elif s:
s += ' '
c += 1
s += b
c += p
print s
</code></pre>
<p>Note that the documentation of the function can be accessed by typing</p>
<pre><code>sage: flowed?
</code></pre>
https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39341#post-id-39341In SageCell, including the documentation string is probably not so useful. So just define
def flowed(a, flow=72):
blocks = str(a).split()
s = blocks.pop(0)
c = len(s) + 1
while blocks:
b = blocks.pop(0)
p = len(b)
if c + p > flow:
print s
s = ''
c = 1
elif s:
s += ' '
c += 1
s += b
c += p
print s
and then use `flowed` to print anything (list or other) with your desired maximum line length.
For instance,
flowed(range(22), flow=32)
will give the following output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22]Mon, 30 Oct 2017 19:01:08 +0100https://ask.sagemath.org/question/39240/how-to-print-the-printout-not-too-long-on-sagecell/?comment=39341#post-id-39341