Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.024 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'e.g.,': 0.07; 'subject:into': 0.09; 'cc:addr:python-list': 0.10; 'line)': 0.16; 'map(int,': 0.16; 'reason.': 0.16; 'sequence.': 0.16; 'subject:Converting': 0.16; 'string': 0.17; 'wrote:': 0.17; 'jan': 0.18; 'file.': 0.20; 'cc:2**0': 0.23; '(i.e.,': 0.23; 'cc:no real name:2**0': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'subject:list': 0.28; "i'm": 0.29; 'everyone': 0.33; 'hi,': 0.33; "can't": 0.34; 'list': 0.35; 'returning': 0.35; 'subject:?': 0.35; 'but': 0.36; 'does': 0.37; 'option': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'sure': 0.38; 'performance': 0.39; 'help': 0.40; 'more': 0.63; 'jul': 0.65; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'quicker': 0.84; 'to:none': 0.93 Date: Sun, 22 Jul 2012 19:20:18 +0300 From: Jan Riechers User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 CC: python-list@python.org Subject: Re: Converting a list of strings into a list of integers? References: <3rCdnUCiWpP1gZHNnZ2dnUVZ7vQAAAAA@giganews.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: janpeterr@freenet.de List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342974153 news.xs4all.nl 6851 [2001:888:2000:d::a6]:54788 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25818 On 22.07.2012 18:39, Alister wrote: > On Sun, 22 Jul 2012 10:29:44 -0500, Tony the Tiger wrote: >> 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 >> >> >> TIA >> >> >> /Grrr > > looks like a classic list comprehension to me and can be achieved in a > single line > > MODUS_LIST=[int(x) for x in options.modus_list] > > > Hi, I am not sure why everyone is using the for-iterator option over a "map", but I would do it like that: MODUS_LIST= map(int, options.modus_list) "map" works on a list and does commandX (here "int" conversion, use "str" for string.. et cetera) on sequenceY, returning a sequence. More in the help file. And if I'm not completely mistaken, it's also the quicker way to do performance wise. But I can't completely recall the exact reason. Jan