Re: use of index (beginner's question)

Discussion in 'Python' started by Chris Angelico, Apr 28, 2011.

  1. 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 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
    Chris Angelico, Apr 28, 2011
    #1
    1. Advertising

  2. 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
    Thomas 'PointedEars' Lahn, Apr 28, 2011
    #2
    1. Advertising

  3. Chris Angelico

    Chris Rebert Guest

    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
    Chris Rebert, Apr 28, 2011
    #3
  4. 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
    Chris Angelico, Apr 28, 2011
    #4
  5. 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
    Algis Kabaila, Apr 28, 2011
    #5
  6. Chris Angelico

    Iain King Guest

    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 Angelico


    I did not know this. Very useful!

    Iain
    Iain King, Apr 28, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    556
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Rensjuh
    Replies:
    7
    Views:
    945
    Mabden
    Sep 2, 2004
  3. jmDesktop
    Replies:
    3
    Views:
    285
  4. Rusty Scalf

    use of index (beginner's question)

    Rusty Scalf, Apr 28, 2011, in forum: Python
    Replies:
    1
    Views:
    184
    Steven D'Aprano
    Apr 28, 2011
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    266
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page