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


Groups > comp.lang.python > #4294

Re: use of index (beginner's question)

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <dan.kluev@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.102
X-Spam-Level *
X-Spam-Evidence '*H*': 0.80; '*S*': 0.00; 'subject:beginner': 0.09; '>>>': 0.12; 'am,': 0.14; 'wrote:': 0.14; '11:42': 0.16; 'variable': 0.21; 'header:In-Reply-To:1': 0.22; 'subject:question': 0.22; 'thu,': 0.22; 'message- id:@mail.gmail.com': 0.28; 'daniel': 0.29; 'list1': 0.31; 'to:addr :python-list': 0.32; 'generally': 0.33; 'skip:" 10': 0.34; 'there': 0.35; 'that,': 0.35; 'print': 0.35; 'subject:use': 0.35; 'case,': 0.36; 'received:209.85': 0.37; 'apr': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'to:addr:python.org': 0.39; 'subject: (': 0.39; 'received:209': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'best': 0.60; '2011': 0.62; 're- design': 0.84; 'received:209.85.210.174': 0.84; 'received:mail- iy0-f174.google.com': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=PYzOetJFn7n4kP1tzBNqV16d2GPXifz9/527TypRBGc=; b=DdqInVt9MH4rrjmPebuS0RBxIQ+Hxv0fl33sPS55GddVz/+Oooy8c85cKaB0kd9lhm cyGBq8VheEiRezQuMTthGVXXoSAJoAH7ojj7dexGORsygdR3VvWSm876EUbXc50y+fcF 4D+r9IhprLEbEGKodYTzFD8ya9FKJrkzxXi+8=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=ub91jBTM9wsEJn8relrtjNWOM8zKYm1zFP+wzpJ48YzPqZZYEmCGQ0CZeQnGyBdXTv kJuCNFdIc0MuSxvRyDlwETy2ZdOOSNniB4fv51ItRXm3aeII5b9C+KBWBDa9pNpSNDTY mjlgYzM3AmwRNDeokmYJHXAG8hVt1vXxy4pmY=
MIME-Version 1.0
In-Reply-To <4DB8B7F6.9040500@sonic.net>
References <4DB8B7F6.9040500@sonic.net>
Date Fri, 29 Apr 2011 15:19:23 +1100
Subject Re: use of index (beginner's question)
From Daniel Kluev <dan.kluev@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.982.1304050771.9059.python-list@python.org> (permalink)
Lines 29
NNTP-Posting-Host 82.94.164.166
X-Trace 1304050771 news.xs4all.nl 41114 [::ffff:82.94.164.166]:57149
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:4294

Show key headers only | View raw


On Thu, Apr 28, 2011 at 11:42 AM, Rusty Scalf <iai-gis@sonic.net> wrote:
> list1 = ['pig', 'horse', 'moose']
> list2 =  ['62327', '49123', '79115']
> n = 2
> s2 = "list" + `n`
> a = s2[list1.index('horse')]
> print a
>
>  -does not work

While advices above are indeed right way to go in your case, there is
a way to get variable by its name.

>>> list2 = ['62327', '49123', '79115']
>>> n = 2
>>> s2 = "list{0}".format(n)
>>> print s2
list2
>>> print locals()[s2]
['62327', '49123', '79115']
>>> print locals()[s2][0]
62327

But generally if you need to do that, you would be better with
re-design of your data/architecture.

-- 
With best regards,
Daniel Kluev

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


Thread

Re: use of index (beginner's question) Daniel Kluev <dan.kluev@gmail.com> - 2011-04-29 15:19 +1100

csiph-web