Re: Converting a list of strings into a list of integers?

Discussion in 'Python' started by Roy Smith, Jul 22, 2012.

  1. Roy Smith

    Roy Smith Guest

    In article <>,
    Tony the Tiger <> wrote:

    > Hi,
    > Is there such a thing in the language, or do I have to invent it myself?
    >
    > I came up with the following:
    >
    > # options.modus_list contains, e.g., "[2,3,4]"
    > # (a string from the command line)
    > # MODUS_LIST contains, e.g., [2,4,8,16]
    > # (i.e., a list of integers)
    >
    > if options.modus_list:
    > intTmp = []
    > modTmp = options.modus_list[1:-1]
    > for itm in modTmp:
    > intTmp.append(int(itm))
    > MODUS_LIST = intTmp


    To answer the question you asked, to convert a list of strings to a list
    of ints, you want to do something like:

    MODUS_LIST = [int(i) for i in options.modus_list]

    But, to answer the question you didn't ask, if you're trying to parse
    command-line arguments, you really want to use the argparse module.
    It's a little complicated to learn, but it's well worth the effort.
     
    Roy Smith, Jul 22, 2012
    #1
    1. Advertising

  2. Roy Smith

    Peter Otten Guest

    Tony the Tiger wrote:

    > On Sun, 22 Jul 2012 11:39:30 -0400, Roy Smith wrote:
    >
    >> To answer the question you asked, to convert a list of strings to a list
    >> of ints, you want to do something like:
    >>
    >> MODUS_LIST = [int(i) for i in options.modus_list]

    >
    > Thanks. I'll look into that. I now remember reading about the technique
    > (in Mark Lutz' "Learning Python"), but it seems I'm getting old as I tend
    > to forget about it from time to time. ;)
    >
    >> But, to answer the question you didn't ask, if you're trying to parse
    >> command-line arguments, you really want to use the argparse module. It's
    >> a little complicated to learn, but it's well worth the effort.

    >
    > Your suggestions about the argparse. Well, it seems it does pretty much
    > the same as OptionParser which I use now. Perhaps it has more features
    > (that I probably won't need in my 30 line script), I only need to keep
    > track of maybe one or two options. Maybe one of these days, when I have
    > little else to do, or when the OptionParser stops working, I'll give it a
    > try. Thanks. :)


    Here's an argparse example:

    $ cat argparse_list.py
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("-m", "--modus", type=int, nargs="*")

    print parser.parse_args().modus
    $ python argparse_list.py
    None
    $ python argparse_list.py -m
    []
    $ python argparse_list.py -m 1
    [1]
    $ python argparse_list.py -m 1 2 3
    [1, 2, 3]
     
    Peter Otten, Jul 22, 2012
    #2
    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. Jan Riechers
    Replies:
    4
    Views:
    223
  2. Paul Rubin
    Replies:
    0
    Views:
    167
    Paul Rubin
    Jul 22, 2012
  3. David Robinow
    Replies:
    0
    Views:
    171
    David Robinow
    Jul 22, 2012
  4. Jan Riechers
    Replies:
    0
    Views:
    159
    Jan Riechers
    Jul 22, 2012
  5. Ian Kelly
    Replies:
    0
    Views:
    144
    Ian Kelly
    Jul 22, 2012
Loading...

Share This Page