Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: use of index (beginner's question)

Reply
Thread Tools

Re: use of index (beginner's question)

 
 
Chris Angelico
Guest
Posts: n/a
 
      04-28-2011
On Thu, Apr 28, 2011 at 10:42 AM, Rusty Scalf <iai-> 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 eval to convert it, but you'd do better
to have a list of lists:

lists = [
['pig', 'horse', 'moose']
*['62327', '49123', '79115']
]

Then you could use:
n = 2
a = lists[n][list1.index('horse')]

If it helps, you can think of this as a two-dimensional array;
technically it's not, though, it's a list that contains other lists.
(Note that you probably don't want to use the word 'list' as a
variable name; it's the name of the type, and is actually a token in
its own right. But 'lists' or something is fine.)

Chris Angelico
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-28-2011
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 the value "list2"; this is not the same as the
> variable list2. You could use eval to convert it, but you'd do better
> to have a list of lists:
>
> lists = [
> ['pig', 'horse', 'moose']
> ['62327', '49123', '79115']
> ]


You forgot a comma after the first `]', to separate the list elements.
A way to reuse the existing code is

lists =[list1, list2]

> Then you could use:
> n = 2
> a = lists[n][list1.index('horse')]


That would throw an IndexError exception, though, since list indexes are
0-based, and this list has only two items (so the highest index is 1, not
2).

But even if this was fixed, this could still throw a ValueError exception
if there was no 'horse' in `list1'. While you could catch that –

needle = 'horse'
try:
a = lists[n][list1.index(needle)]
except ValueError:
a = 'N/A'

– perhaps a better way to store this data is a dictionary:

data = {
'pig': '62327',
'horse': '49123',
'moose': '79115'
}

print data.get('horse')
print data.get('cow')
print data.get('cow', 'N/A')

Such a dictionary can be built from the existing lists as well:

data = dict(zip(list1, list2))
print data
print data.get('horse', 'N/A')


HTH
--
PointedEars
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      04-28-2011
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 = "list%s" % n


Agreed. The backticks operator is deprecated and was removed in Python 3.

Cheers,
Chris
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      04-28-2011
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: Run the code through IDLE before posting it,
saves people a lot of trouble!

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 Angelico
 
Reply With Quote
 
Algis Kabaila
Guest
Posts: n/a
 
      04-28-2011
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 you?

OldAl.
--
Algis
http://akabaila.pcug.org.au/StructuralAnalysis.pdf
 
Reply With Quote
 
Iain King
Guest
Posts: n/a
 
      04-28-2011
On Apr 28, 2:45*am, Chris Angelico <ros...@gmail.com> 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 Angelico


I did not know this. Very useful!

Iain
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" camelean@shaw.ca ASP .Net 3 02-22-2011 07:06 PM
Program Index: cannot view entire index XP Luke O'Malley Computer Support 2 05-05-2008 03:34 AM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
index.htm or index.html ? Robert Cooze NZ Computing 15 12-13-2005 05:53 PM
problem with index.html .(page is automatically gettin redirected to index.html) karthikeyavenkat Java 2 03-17-2005 10:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57