Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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]