simplify printing of a list

Discussion in 'Python' started by beliavsky@aol.com, Jun 3, 2004.

  1. Guest

    To print a list with a specified format one can write (for example)

    for j in [0,1,2]:
    print "%6d"%j,
    print

    The code

    print "%6d"%[0,1,2]

    currently produces a syntax error, but it would be convenient if it
    had the same meaning as the loop above.

    One can write a function to print a list, for example

    def print_list(x,fmt_x="%6d"):
    """ print a list on one line """
    for y in x: print fmt_x % y,

    print_list([0,1,2])

    but it gets messy to print several lists on the same line.

    In Fortran 90/95 one can write

    print "(100i6)",(/0,1,2/)

    where the format (100i6) means that UP TO 100 integers are printed
    using 6 columns. An alternative suggestion I have for Python is to
    allow

    print "100%6d"%[0,1,2]

    with the same meaning.

    I realize that what I am asking for is just a convenience, but it is
    one that I could use in almost every program I write.
     
    , Jun 3, 2004
    #1
    1. Advertising

  2. Eru Guest

    escribio:
    > To print a list with a specified format one can write (for example)
    >
    > for j in [0,1,2]:
    > print "%6d"%j,
    > print
    >
    > The code
    >
    > print "%6d"%[0,1,2]
    >
    > currently produces a syntax error, but it would be convenient if it
    > had the same meaning as the loop above.
    >
    > One can write a function to print a list, for example
    >
    > def print_list(x,fmt_x="%6d"):
    > """ print a list on one line """
    > for y in x: print fmt_x % y,
    >
    > print_list([0,1,2])


    How about using:

    def fmtlst(fmt,lst):
    return fmt*len(lst) % tuple(lst)

    print fmtlst("%3d",range(5)), fmtlst("%5.2f", [1.2, 3, 4.567])

    This prints:

    0 1 2 3 4 1.20 3.00 4.57

    Maybe it gets your job done (though it's not the most efficient thing
    one can do...)

    >
    > but it gets messy to print several lists on the same line.
    >
    > In Fortran 90/95 one can write
    >
    > print "(100i6)",(/0,1,2/)
    >
    > where the format (100i6) means that UP TO 100 integers are printed
    > using 6 columns. An alternative suggestion I have for Python is to
    > allow
    >
    > print "100%6d"%[0,1,2]


    It looks a bit weird (unpythonic) to me, and we can use workarounds :)

    >
    > with the same meaning.
    >
    > I realize that what I am asking for is just a convenience, but it is
    > one that I could use in almost every program I write.


    --
    Daniel Ripolles ( Eru )
    Make Source, Not War
    for(0..pop){for($c=$_%2;$_>>=1;){$c=$_%2 .$c}print"$c\n"}
     
    Eru, Jun 3, 2004
    #2
    1. Advertising

  3. Larry Bates Guest

    How about:

    print ''.join(["%6d" % j for j in [0,1,2]])

    or

    print reduce(lambda x,y: x+"%6d" % y, [0,1,2], '')

    or

    t=[sys.stdout.write("%6d" % j for j in [0,1,2]]
    (note: the t assignment is so the list comprehension
    results don't print)

    all work and are pythonic in nature. I didn't
    test, but I'll be the last one is the most
    efficient.

    HTH,
    Larry Bates
    Syscon, Inc.

    <> wrote in message
    news:...
    > To print a list with a specified format one can write (for example)
    >
    > for j in [0,1,2]:
    > print "%6d"%j,
    > print
    >
    > The code
    >
    > print "%6d"%[0,1,2]
    >
    > currently produces a syntax error, but it would be convenient if it
    > had the same meaning as the loop above.
    >
    > One can write a function to print a list, for example
    >
    > def print_list(x,fmt_x="%6d"):
    > """ print a list on one line """
    > for y in x: print fmt_x % y,
    >
    > print_list([0,1,2])
    >
    > but it gets messy to print several lists on the same line.
    >
    > In Fortran 90/95 one can write
    >
    > print "(100i6)",(/0,1,2/)
    >
    > where the format (100i6) means that UP TO 100 integers are printed
    > using 6 columns. An alternative suggestion I have for Python is to
    > allow
    >
    > print "100%6d"%[0,1,2]
    >
    > with the same meaning.
    >
    > I realize that what I am asking for is just a convenience, but it is
    > one that I could use in almost every program I write.
     
    Larry Bates, Jun 4, 2004
    #3
  4. Peter Otten Guest

    wrote:

    > To print a list with a specified format one can write (for example)
    >
    > for j in [0,1,2]:
    > print "%6d"%j,
    > print
    >
    > The code
    >
    > print "%6d"%[0,1,2]
    >
    > currently produces a syntax error, but it would be convenient if it
    > had the same meaning as the loop above.


    A TypeError. What if I wanted the current behaviour, e. g

    >>> "%s" % [1, 2, 3]

    '[1, 2, 3]'

    instead of

    >>> "%s" % [1, 2, 3]

    '1 2 3' #faked

    >
    > One can write a function to print a list, for example
    >
    > def print_list(x,fmt_x="%6d"):
    > """ print a list on one line """
    > for y in x: print fmt_x % y,
    >
    > print_list([0,1,2])
    >
    > but it gets messy to print several lists on the same line.


    How about different operators for the two formatting operations:

    >>> class Format(str):

    .... def __mul__(self, other):
    .... return " ".join(map(self.__mod__, other))
    ....
    >>>
    >>> Format("%6d") % 1

    ' 1'
    >>> Format("%6d") * [1, 2, 3]

    ' 1 2 3'
    >>>


    Peter
     
    Peter Otten, Jun 4, 2004
    #4
  5. Jeff Epler Guest

    On Thu, Jun 03, 2004 at 03:08:57PM -0700, wrote:
    > To print a list with a specified format one can write (for example)
    >
    > for j in [0,1,2]:
    > print "%6d"%j,
    > print
    >
    > The code
    >
    > print "%6d"%[0,1,2]
    >
    > currently produces a syntax error,


    No, it doesn't. (it produces a TypeError)

    > but it would be convenient if it
    > had the same meaning as the loop above.


    No, it wouldn't.

    [remainder deleted]

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (GNU/Linux)

    iD8DBQFAwGYgJd01MZaTXX0RAna7AKCNCDwhmu6BQKca2vB1thu8x9dQYgCfVMbJ
    FIshxg3PLUA9HV/OTi0Cbko=
    =PV6m
    -----END PGP SIGNATURE-----
     
    Jeff Epler, Jun 4, 2004
    #5
  6. > print "100%6d"%[0,1,2]

    The real problem is that "100%6d" already has a meaning in string
    formatting, though not what you like. On the most part, Python string
    formatting conventions follow C string formatting conventions. Python
    has added "%(key)s" formatting, and there may be $-formatting in the
    future (I stopped following that thread months ago).

    Again, the syntax you offer "100%6d" is already valid, if meaning
    something else.

    - Josiah
     
    Josiah Carlson, Jun 8, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juan Carlos Allica
    Replies:
    2
    Views:
    677
    Noway2
    Jan 25, 2006
  2. Gerhard Rapp

    How to simplify logical expressions?

    Gerhard Rapp, Jul 29, 2003, in forum: Java
    Replies:
    2
    Views:
    1,229
    Peter Schoaff
    Jul 30, 2003
  3. Dean Rettig
    Replies:
    2
    Views:
    7,755
    Oscar Kind
    Jan 6, 2004
  4. davout
    Replies:
    5
    Views:
    739
    Bryce
    Aug 17, 2004
  5. Replies:
    7
    Views:
    516
Loading...

Share This Page