Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Converting a list of strings into a list of integers?

Reply
Thread Tools

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

 
 
Roy Smith
Guest
Posts: n/a
 
      07-22-2012
In article <(E-Mail Removed)>,
Tony the Tiger <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      07-22-2012
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]


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Converting a list of strings into a list of integers? Jan Riechers Python 4 07-23-2012 03:31 PM
Re: Converting a list of strings into a list of integers? Ian Kelly Python 0 07-22-2012 05:16 PM
Re: Converting a list of strings into a list of integers? Jan Riechers Python 0 07-22-2012 05:10 PM
Re: Converting a list of strings into a list of integers? David Robinow Python 0 07-22-2012 05:03 PM
Re: Converting a list of strings into a list of integers? Paul Rubin Python 0 07-22-2012 05:03 PM



Advertisments