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


Groups > comp.lang.python > #4294 > unrolled thread

Re: use of index (beginner's question)

Started byDaniel Kluev <dan.kluev@gmail.com>
First post2011-04-29 15:19 +1100
Last post2011-04-29 15:19 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#4294 — Re: use of index (beginner's question)

FromDaniel Kluev <dan.kluev@gmail.com>
Date2011-04-29 15:19 +1100
SubjectRe: use of index (beginner's question)
Message-ID<mailman.982.1304050771.9059.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web