473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

replace deepest level of nested list

I have a list of lists, N+1 deep.
Like this (for N=2):
[[['r00','g00','b00'],['r01','g01','b01']],[['r10','g10','b10'],['r11','g11'
,'b11']]]

I want to efficiently produce the same structure
except that the utlimate lists are replaced by a chosen (by index) item.
E.g.,
[['r00','r01'],['r10','r11']]

N is not known ahead of time.

Suggestions?

Thanks,
Alan Isaac
Sep 4 '06 #1
4 1695
David Isaac wrote:
I have a list of lists, N+1 deep.
Like this (for N=2):
[[['r00','g00','b00'],['r01','g01','b01']],[['r10','g10','b10'],['r11','g11'
,'b11']]]

I want to efficiently produce the same structure
except that the utlimate lists are replaced by a chosen (by index) item.
E.g.,
[['r00','r01'],['r10','r11']]

N is not known ahead of time.

Suggestions?

Thanks,
Alan Isaac
Numeric/Numpy is ideal for this:

from Numeric import array

def slicelist(nestedlist,index):

a = array(nestedlist,'O')

# return the slice a[:,:,...,:,index]

fullindex = [slice(None)] * len(a.shape)

fullindex[-1] = index

return a[fullindex].tolist()
George

Sep 4 '06 #2
David Isaac wrote:
I have a list of lists, N+1 deep.
Like this (for N=2):
[[['r00','g00','b00'],['r01','g01','b01']],[['r10','g10','b10'],['r11','g11'
,'b11']]]

I want to efficiently produce the same structure
except that the utlimate lists are replaced by a chosen (by index) item.
E.g.,
[['r00','r01'],['r10','r11']]

N is not known ahead of time.
First thing I came up with:
>>l = [[['r00','g00','b00'],['r01','g01','b01']],[['r10','g10','b10'],['r11','g11','b11']]]
def get_deepest(l, n):
.... if isinstance(l[0], list):
.... return [get_deepest(s, n) for s in l]
.... else:
.... return l[n]
....
>>get_deepest(l, 0)
[['r00', 'r01'], ['r10', 'r11']]
>>get_deepest(l, 1)
[['g00', 'g01'], ['g10', 'g11']]
>>>
n is the chosen index.
HTH.
--
Roberto Bonvallet
Sep 4 '06 #3
Thanks to both Roberto and George.
I had considered the recursive solution
but was worried about its efficiency.
I had not seen how to implement the numpy
solution, which looks pretty nice.

Thanks!
Alan
Sep 4 '06 #4
David Isaac wrote:
Thanks to both Roberto and George.
I had considered the recursive solution
but was worried about its efficiency.
I had not seen how to implement the numpy
solution, which looks pretty nice.

Thanks!
Alan

You could also use pyarray, which mimics numpy's indexing, but uses native
python lists as the data store.

So...
>>from pyarray import ndlist
source =
ndlist([[['r00','g00','b00'],['r01','g01','b01']],[['r10','g10','b10'],['r11','g11'
.... ,'b11']]])
>>source
array([[[r00, g00, b00],
[r01, g01, b01]],

[[r10, g10, b10],
[r11, g11, b11]]])
>>source[...,0] # note elipsis means skip all but the last dimension
array([[r00, r01],
[r10, r11]])
>>_.tolist()
[['r00', 'r01'], ['r10', 'r11']]
>>>
pyarray (single pure-python module) can be downloaded from:

http://svn.brownspencer.com/pyarray/trunk/

Regards

Michael

Sep 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
3
by: Br | last post by:
I'm going to go into a fair bit of detail as I'm hoping my methods may be of assistance to anyone else wanting to implement something similar (or totally confusing:) One of systems I've...
5
by: Jeroen Ceuppens | last post by:
I need to put a new node at the end of the tree, that end is not te lowest in de list but the deepest (the one with the most + before it) Node A Node 1 Node 2 Node 3 Node 4: Deepest Node B:...
6
by: Brian Worth | last post by:
I've just upgraded to VB .NET 2002 Standard. I had an old VB4 program which consisted of a form containing DriveListBox and a DirListBox and a Go button. The program would allow me to select a...
3
by: John Williams | last post by:
I have a HTML page consisting of several tables at the top level of the DOM, interpersed with other HTML tags. For example: HTML BODY -- SCRIPT -- SCRIPT -- #comment -- SCRIPT -- A
6
by: deko | last post by:
How do I construct an XHTML-compliant nested unordered list? This displays correctly (both FF and IE): <ul> <li>list item</li> <li>list item</li> <li>list item</li> <ul> <li>nested list...
1
by: Bllich | last post by:
can anyone post an algoritam for the deepest level of a treeView ? it should be a recursive function I think.. I don't need treeView.Nodes.Level because my users don't select the nodes... I...
7
by: patrick j | last post by:
Hi I'm wondering about lists with nested lists as one does on a Saturday afternoon. Anyway below is an example of a list with a nested list which the iCab browser's very useful HTML...
1
by: mlaw40 | last post by:
I am building a CMS driven website in php and I have a list of terms that for each page content I need to search for and replace with the following code: <a href=""></a> The problem I have is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.