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 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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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);
}
|
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...
|
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:...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| |