Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

retrieve item from nested list given index tuple

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


alan.isaac at gmail

Aug 14, 2009, 8:54 AM

Post #1 of 6 (2193 views)
Permalink
retrieve item from nested list given index tuple

`lst` is a nested list

`tpl` is the indexes for an item in the list

What is the nice way to retrieve the item?
(Speedy access is nice.)

I don't want to use NumPy, but I'd like somehow
to avoid an explicit loop. I did consider using
eval. E.g., eval('lst' + '[%d]'*len(tpl)%tpl).
It works but seems rather ugly. I kind of like
reduce(list.__getitem__, tpl, lst) but the
reliance on reduce remains controversial enough
to see i removed from the Python 3 built-ins ...

Thanks,
Alan Isaac
--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Aug 14, 2009, 10:09 AM

Post #2 of 6 (2127 views)
Permalink
Re: retrieve item from nested list given index tuple [In reply to]

On Fri, 14 Aug 2009 15:54:54 +0000, Alan G Isaac wrote:

> `lst` is a nested list
>
> `tpl` is the indexes for an item in the list

> What is the nice way to retrieve the item? (Speedy access is nice.)

Assuming you want to do this frequently, write a helper function, then
use it:

# Untested
def extract(nested, indexes):
for index in indexes:
nested = nested[index]
return nested


> I don't want to use NumPy, but I'd like somehow to avoid an explicit
> loop. I did consider using eval. E.g., eval('lst' +
> '[%d]'*len(tpl)%tpl). It works but seems rather ugly.

And slow.


> I kind of like
> reduce(list.__getitem__, tpl, lst) but the reliance on reduce remains
> controversial enough to see i removed from the Python 3 built-ins ...

It's just moved into functools.


>>> lst = ['a', 'b', ['aa', ['aaa', 'bbb', 'ccc'], 'cc']]
>>> from functools import reduce
>>> lst = ['a', 'b', ['aa', ['aaa', 'bbb', 'ccc'], 'cc']]
>>> reduce(list.__getitem__, (2, 1, 0), lst)
'aaa'


However, it doesn't work too well as soon as you mix sequence types:

>>> reduce(list.__getitem__, (2, 1, 0, 0), lst)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor '__getitem__' requires a 'list' object but received
a 'str'

Try this instead:

>>> from operator import getitem
>>> reduce(getitem, (2, 1, 0), lst)
'aaa'
>>> reduce(getitem, (2, 1, 0, 0), lst)
'a'

operator.getitem is less ugly too.



--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


cjw at ncf

Aug 14, 2009, 10:57 AM

Post #3 of 6 (2132 views)
Permalink
Re: retrieve item from nested list given index tuple [In reply to]

Steven D'Aprano wrote:
> On Fri, 14 Aug 2009 15:54:54 +0000, Alan G Isaac wrote:
>
>> `lst` is a nested list
>>
>> `tpl` is the indexes for an item in the list
>
>> What is the nice way to retrieve the item? (Speedy access is nice.)
>
> Assuming you want to do this frequently, write a helper function, then
> use it:
>
> # Untested
> def extract(nested, indexes):
> for index in indexes:
> nested = nested[index]
> return nested

This looks OK for the first level of nesting. We are not told much about tpl
but suppose that:

lst= [a, [b, [c, d]], [e, f]] and that we wish to retrieve d and f from lst.
tpl would need to be something like [[1, 1, 1], [2, 1]].

If that is the requirement, then Untested is only a step along the road,
extract could be made recursive.

Colin W.
>
>
>> I don't want to use NumPy, but I'd like somehow to avoid an explicit
>> loop. I did consider using eval. E.g., eval('lst' +
>> '[%d]'*len(tpl)%tpl). It works but seems rather ugly.
>
> And slow.
>
>
>> I kind of like
>> reduce(list.__getitem__, tpl, lst) but the reliance on reduce remains
>> controversial enough to see i removed from the Python 3 built-ins ...
>
> It's just moved into functools.
>
>
>>>> lst = ['a', 'b', ['aa', ['aaa', 'bbb', 'ccc'], 'cc']]
>>>> from functools import reduce
>>>> lst = ['a', 'b', ['aa', ['aaa', 'bbb', 'ccc'], 'cc']]
>>>> reduce(list.__getitem__, (2, 1, 0), lst)
> 'aaa'
>
>
> However, it doesn't work too well as soon as you mix sequence types:
>
>>>> reduce(list.__getitem__, (2, 1, 0, 0), lst)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: descriptor '__getitem__' requires a 'list' object but received
> a 'str'
>
> Try this instead:
>
>>>> from operator import getitem
>>>> reduce(getitem, (2, 1, 0), lst)
> 'aaa'
>>>> reduce(getitem, (2, 1, 0, 0), lst)
> 'a'
>
> operator.getitem is less ugly too.
>
>

Colin W.
--
http://mail.python.org/mailman/listinfo/python-list


alan.isaac at gmail

Aug 14, 2009, 11:02 AM

Post #4 of 6 (2129 views)
Permalink
Re: retrieve item from nested list given index tuple [In reply to]

On 8/14/2009 1:09 PM Steven D'Aprano apparently wrote:
> Try this instead:
>
>>>> from operator import getitem
>>>> reduce(getitem, (2, 1, 0), lst)
> 'aaa'
>>>> reduce(getitem, (2, 1, 0, 0), lst)
> 'a'
>
> operator.getitem is less ugly too.


Yes, that's better.
Thanks,
Alan
--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

Aug 14, 2009, 12:32 PM

Post #5 of 6 (2123 views)
Permalink
Re: retrieve item from nested list given index tuple [In reply to]

Colin J. Williams wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Steven
> D'Aprano wrote:
>> On Fri, 14 Aug 2009 15:54:54 +0000, Alan G Isaac wrote:
>>
>>> `lst` is a nested list
>>>
>>> `tpl` is the indexes for an item in the list
>>
>>> What is the nice way to retrieve the item? (Speedy access is nice.)
>>
>> Assuming you want to do this frequently, write a helper function,
>> then use it:
>>
>> # Untested
>> def extract(nested, indexes):
>> for index in indexes:
>> nested = nested[index]
>> return nested
>
> This looks OK for the first level of nesting. We are not told much
> about tpl but suppose that:
>
> lst= [a, [b, [c, d]], [e, f]] and that we wish to retrieve d and f
> from lst. tpl would need to be something like [[1, 1, 1], [2, 1]].
>
> If that is the requirement, then Untested is only a step along the
> road, extract could be made recursive.
>
> Colin W.
> <snip>

You missed the point: he's retrieving *an* item from a list that's
nested arbitrarily. Each item in the tpl (tuple) is a simple integer.


DaveA

--
http://mail.python.org/mailman/listinfo/python-list


cjwilliams43 at gmail

Aug 14, 2009, 12:55 PM

Post #6 of 6 (2123 views)
Permalink
Re: retrieve item from nested list given index tuple [In reply to]

Dave Angel wrote:
> Colin J. Williams wrote:
>> <div class="moz-text-flowed" style="font-family: -moz-fixed">Steven
>> D'Aprano wrote:
>>> On Fri, 14 Aug 2009 15:54:54 +0000, Alan G Isaac wrote:
>>>
>>>> `lst` is a nested list
>>>>
>>>> `tpl` is the indexes for an item in the list
>>>
>>>> What is the nice way to retrieve the item? (Speedy access is nice.)
>>>
>>> Assuming you want to do this frequently, write a helper function,
>>> then use it:
>>>
>>> # Untested
>>> def extract(nested, indexes):
>>> for index in indexes:
>>> nested = nested[index]
>>> return nested
>>
>> This looks OK for the first level of nesting. We are not told much
>> about tpl but suppose that:
>>
>> lst= [a, [b, [c, d]], [e, f]] and that we wish to retrieve d and f
>> from lst. tpl would need to be something like [[1, 1, 1], [2, 1]].
>>
>> If that is the requirement, then Untested is only a step along the
>> road, extract could be made recursive.
>>
>> Colin W.
>> <snip>
>
> You missed the point: he's retrieving *an* item from a list that's
> nested arbitrarily. Each item in the tpl (tuple) is a simple integer.
>
>
> DaveA
Yes, you are right.

Colin W.
>
>
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.