Thank You Re: use of index (beginner's question)

2011-04-29 Thread Rusty Scalf
An overdue Thank You to everyone who responded. I got well more than I bargained for, including needed reinforcement (beyond the beginner's guides) of how Python actually works and some good programming habits. I am grateful. I liked Steven D'Aprano comment: Define "does not work". Wha

Re: use of index (beginner's question)

2011-04-28 Thread Daniel Kluev
On Thu, Apr 28, 2011 at 11:42 AM, Rusty Scalf 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 g

Re: use of index (beginner's question)

2011-04-28 Thread Rhodri James
On Thu, 28 Apr 2011 01:49:33 +0100, Chris Angelico wrote: On Thu, Apr 28, 2011 at 10:42 AM, Rusty Scalf wrote: list1 = ['pig', 'horse', 'moose'] list2 = ['62327', '49123', '79115'] n = 2 s2 = "list" + `n` a = s2[list1.index('horse')] print a s2 is a string with the value "list2"; this is n

Re: use of index (beginner's question) (Iain King)

2011-04-28 Thread Apprentice3D
lists instead of strings (Andrew Berg) > 4. Re: unpickling derived LogRecord in python 2.7 from python2.6 > (Peter Otten) > 5. Re: Access violation reading 0x0010 (yuan zheng) > 6. Re: argparse parser stores lists instead of strings (Peter Otten) > 7. Re: use of index (

Re: use of index (beginner's question)

2011-04-28 Thread Graeme Glass
On Apr 28, 5:32 am, Algis Kabaila wrote: > On Thursday 28 April 2011 11:23:51 Thomas 'PointedEars' Lahn > wrote:> Chris Angelico wrote: > > > Rusty Scalf wrote: > > >> list1 = ['pig', 'horse', 'moose'] > > >> list2 =  ['62327', '49123', '79115'] > > >> n = 2 > > >> s2 = "list" + `n` > >>> "list" +

Re: use of index (beginner's question)

2011-04-28 Thread Iain King
On Apr 28, 2:45 am, Chris Angelico wrote: > Incidentally, you're allowed to put the comma on the last item too: > >  lists = [ >   ['pig', 'horse', 'moose'], >   ['62327', '49123', '79115'], > ] > > Often makes for easier maintenance, especially when you append > array/list elements. > > Chris Ang

Re: use of index (beginner's question)

2011-04-27 Thread Algis Kabaila
On Thursday 28 April 2011 11:23:51 Thomas 'PointedEars' Lahn wrote: > Chris Angelico wrote: > > Rusty Scalf wrote: > >> list1 = ['pig', 'horse', 'moose'] > >> list2 = ['62327', '49123', '79115'] > >> n = 2 > >> s2 = "list" + `n` >>> "list" + 'n' 'listn' >>> And IMHO you did not want that, did yo

Re: use of index (beginner's question)

2011-04-27 Thread Steven D'Aprano
On Wed, 27 Apr 2011 17:42:30 -0700, Rusty Scalf wrote: > Greetings, > I am just now learning python and am trying to use the index function > with variables. > > list1 = ['pig', 'horse', 'moose'] > list2 = ['62327', '49123', '79115'] > a = list2[list1.index('horse')] > print a > >49123 > >

Re: use of index (beginner's question)

2011-04-27 Thread Chris Angelico
On Thu, Apr 28, 2011 at 11:23 AM, Thomas 'PointedEars' Lahn wrote: > You forgot a comma after the first `]', to separate the list elements. Whoops! Apologies. It's very confusing when example code has silly bugs in it! And yes, need to either back down the indices or insert a shim. Memo, to self:

Re: use of index (beginner's question)

2011-04-27 Thread Chris Rebert
On Wed, Apr 27, 2011 at 6:23 PM, Thomas 'PointedEars' Lahn wrote: > Chris Angelico wrote: >> Rusty Scalf wrote: >>> list1 = ['pig', 'horse', 'moose'] >>> list2 =  ['62327', '49123', '79115'] >>> n = 2 >>> s2 = "list" + `n` > > I would prefer the clearer > >  s2 = "list" + str(n) > > or > >  s2 = "

Re: use of index (beginner's question)

2011-04-27 Thread Thomas 'PointedEars' Lahn
Chris Angelico wrote: > Rusty Scalf wrote: >> list1 = ['pig', 'horse', 'moose'] >> list2 = ['62327', '49123', '79115'] >> n = 2 >> s2 = "list" + `n` I would prefer the clearer s2 = "list" + str(n) or s2 = "list%s" % n >> a = s2[list1.index('horse')] >> print a > > s2 is a string with th

Re: use of index (beginner's question)

2011-04-27 Thread Chris Angelico
On Thu, Apr 28, 2011 at 10:42 AM, Rusty Scalf wrote: > list1 = ['pig', 'horse', 'moose'] > list2 =  ['62327', '49123', '79115'] > n = 2 > s2 = "list" + `n` > a = s2[list1.index('horse')] > print a s2 is a string with the value "list2"; this is not the same as the variable list2. You could use eva

use of index (beginner's question)

2011-04-27 Thread Rusty Scalf
Greetings, I am just now learning python and am trying to use the index function with variables. list1 = ['pig', 'horse', 'moose'] list2 = ['62327', '49123', '79115'] a = list2[list1.index('horse')] print a >49123 -works fine. But list1 = ['pig', 'horse', 'moose'] list2 = ['62327', '4912