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


Groups > comp.lang.python > #4294

Re: use of index (beginner's question)

References <4DB8B7F6.9040500@sonic.net>
Date 2011-04-29 15:19 +1100
Subject Re: use of index (beginner's question)
From Daniel Kluev <dan.kluev@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.982.1304050771.9059.python-list@python.org> (permalink)

Show all headers | 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