473,543 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replace deepest level of nested list

I have a list of lists, N+1 deep.
Like this (for N=2):
[[['r00','g00','b0 0'],['r01','g01','b0 1']],[['r10','g10','b1 0'],['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 1701
David Isaac wrote:
I have a list of lists, N+1 deep.
Like this (for N=2):
[[['r00','g00','b0 0'],['r01','g01','b0 1']],[['r10','g10','b1 0'],['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(neste dlist,index):

a = array(nestedlis t,'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','b0 0'],['r01','g01','b0 1']],[['r10','g10','b1 0'],['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','b0 0'],['r01','g01','b0 1']],[['r10','g10','b1 0'],['r11','g11','b1 1']]]
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','b0 0'],['r01','g01','b0 1']],[['r10','g10','b1 0'],['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
2642
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
3292
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 developed has three levels of security. Admins - can see all records Manager - can only see records based on an organisation structure held in a table...
5
3283
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: not this one
6
3193
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 drive and navigate through to the lowest level directory of a tree and return the full path down to the lowest level. The program would then obtain...
3
4262
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
15411
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 item</li>
1
5082
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 need to get a random node from the whole collection so I want my rndLevel = random.next(deepest level) rndNode = random.next(number of nodes on that...
7
3632
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 verification ability will not like: <ul> <li><a href="#">link</a></li>
1
1558
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 that a term can appear in another term, for example garden furniture, hire garden furniture. When I am doing a basic preg_replace it means that the...
0
7411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7354
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5282
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4898
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
643
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.