Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25813

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

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Converting a list of strings into a list of integers?
Date 2012-07-22 11:39 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-7DD3BA.11393022072012@news.panix.com> (permalink)
References <3rCdnUCiWpP1gZHNnZ2dnUVZ7vQAAAAA@giganews.com>

Show all headers | View raw


In article <3rCdnUCiWpP1gZHNnZ2dnUVZ7vQAAAAA@giganews.com>,
 Tony the Tiger <tony@tiger.invalid> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Converting a list of strings into a list of integers? Tony the Tiger <tony@tiger.invalid> - 2012-07-22 10:29 -0500
  Re: Converting a list of strings into a list of integers? Roy Smith <roy@panix.com> - 2012-07-22 11:39 -0400
    Re: Converting a list of strings into a list of integers? Tony the Tiger <tony@tiger.invalid> - 2012-07-22 11:01 -0500
      Re: Converting a list of strings into a list of integers? Peter Otten <__peter__@web.de> - 2012-07-22 18:30 +0200
  Re: Converting a list of strings into a list of integers? Alister <alister.ware@ntlworld.com> - 2012-07-22 15:39 +0000
    Re: Converting a list of strings into a list of integers? Tony the Tiger <tony@tiger.invalid> - 2012-07-22 11:01 -0500
    Re: Converting a list of strings into a list of integers? Jan Riechers <janpeterr@freenet.de> - 2012-07-22 19:20 +0300
      Re: Converting a list of strings into a list of integers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-22 17:01 +0000
        Re: Converting a list of strings into a list of integers? Jan Riechers <janpeterr@freenet.de> - 2012-07-22 20:27 +0300
      Re: Converting a list of strings into a list of integers? Grant Edwards <invalid@invalid.invalid> - 2012-07-23 14:27 +0000
        Re: Converting a list of strings into a list of integers? rusi <rustompmody@gmail.com> - 2012-07-23 08:31 -0700
    Re: Converting a list of strings into a list of integers? David Robinow <drobinow@gmail.com> - 2012-07-22 13:03 -0400
    Re: Converting a list of strings into a list of integers? Jan Riechers <janpeterr@freenet.de> - 2012-07-22 20:10 +0300
    Re: Converting a list of strings into a list of integers? Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-22 11:16 -0600
  Re: Converting a list of strings into a list of integers? Paul Rubin <no.email@nospam.invalid> - 2012-07-22 10:03 -0700
  Re: Converting a list of strings into a list of integers? Dave Angel <d@davea.name> - 2012-07-22 21:03 -0400

csiph-web