use of index (beginner's question)
Daniel Kluev
dan.kluev at gmail.com
Fri Apr 29 00:19:23 EDT 2011
On Thu, Apr 28, 2011 at 11:42 AM, Rusty Scalf <iai-gis at 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
More information about the Python-list
mailing list