Ask Your Question
1

how to print the printout not too long on Sagecell

asked 2017-10-21 17:01:06 +0200

Niyamabrata gravatar image

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...

edit retag flag offensive close merge delete

Comments

myList = range(64) is enough to define that list.

dan_fulea gravatar imagedan_fulea ( 2017-10-22 18:15:09 +0200 )edit

3 Answers

Sort by ยป oldest newest most voted
1

answered 2017-10-22 18:16:00 +0200

dan_fulea gravatar image

Let 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]
edit flag offensive delete link more

Comments

Thanks you so much...i like the second one...its works nice on my list. God gives u the best..

Niyamabrata gravatar imageNiyamabrata ( 2017-10-23 10:13:00 +0200 )edit
0

answered 2017-10-22 05:12:28 +0200

calc314 gravatar image

How about:

num_rows=4
for i in range(num_rows):
    print [myList[i*64/num_rows+j] for j in range(64/num_rows)]
edit flag offensive delete link more

Comments

what 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)]
Niyamabrata gravatar imageNiyamabrata ( 2017-10-22 06:13:55 +0200 )edit

This is a completely new question... (a more general one.)

dan_fulea gravatar imagedan_fulea ( 2017-10-22 18:15:54 +0200 )edit
0

answered 2017-10-23 05:34:04 +0200

slelievre gravatar image

To 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?
edit flag offensive delete link more

Comments

i cant find flowed function on sagecell...

Niyamabrata gravatar imageNiyamabrata ( 2017-10-23 10:13:37 +0200 )edit

Hi, 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.

slelievre gravatar imageslelievre ( 2017-10-30 02:13:20 +0200 )edit

In 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]
slelievre gravatar imageslelievre ( 2017-10-30 19:01:08 +0200 )edit

Thank you so much...i'm new to this apps

Niyamabrata gravatar imageNiyamabrata ( 2017-11-02 10:18:10 +0200 )edit

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-10-21 17:01:06 +0200

Seen: 388 times

Last updated: Nov 02 '17