Hi - I want to take something like ...
lstIn = []
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 10, 'LEA_AUTOID': 1000})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2000})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2001})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2003})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3000})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3001})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3002})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3003})
lstIn.append({'COM_AUTOID': 2, 'PRG_AUTOID': 110, 'LEA_AUTOID': 4000})
.... and produce something like ...
sampleOut =
[[1,[10,1000]],[1,[11,[2000,2001,2003]]],[1,[12,[3000,3001,3002,3003]]],[2,[110,4000]]
Well I've now been around the block a few times with this one and I'm
still frowning !! In the process my code has become uglier and uglier -
I'm sure there must be quite an elegant way of dealing with it - could
anyone give me a push in the right direction ?
Just to provide some motivation here - I should just say that this is
cut down test case - the real problem involves creating a Javascript
structure which in turn defines a three level menu.
The resulting JS will be something like this, I think you can see how
the nested loops get into it.:
var MENU_ITEMS = [
{ pos:'relative', leveloff:[b,a], itemoff:[d,c], size:[e,f], ... },
{ ...Item 1... },
{ ...Item 2... ,
sub:[
{ ...level format... },
{ ...Item 1... },
{ ...Item 2... },
{ ...Item 3... ,
sub:[
{ ...level format... },
{ ...Item 1... },
{ ...Item 2... },
{ ...Item 3... },
{ ...Item 4... }
]
},
{ ...Item 4... },
]
},
{ ...Item 3... }
];
Interested to hear of any smart/elegant ideas.
thanks
Richard. 2 1955 sh*********@gmail.com wrote: Hi - I want to take something like ...
lstIn = [] lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 10, 'LEA_AUTOID': 1000}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2000}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2001}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2003}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3000}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3001}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3002}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3003}) lstIn.append({'COM_AUTOID': 2, 'PRG_AUTOID': 110, 'LEA_AUTOID': 4000})
... and produce something like ...
sampleOut = [[1,[10,1000]],[1,[11,[2000,2001,2003]]],[1,[12,[3000,3001,3002,3003]]],[2,[110,4000]]
Well I've now been around the block a few times with this one and I'm still frowning !! In the process my code has become uglier and uglier - I'm sure there must be quite an elegant way of dealing with it - could anyone give me a push in the right direction ?
Just to provide some motivation here - I should just say that this is cut down test case - the real problem involves creating a Javascript structure which in turn defines a three level menu.
The resulting JS will be something like this, I think you can see how the nested loops get into it.:
var MENU_ITEMS = [ { pos:'relative', leveloff:[b,a], itemoff:[d,c], size:[e,f], ... }, { ...Item 1... }, { ...Item 2... , sub:[ { ...level format... }, { ...Item 1... }, { ...Item 2... }, { ...Item 3... , sub:[ { ...level format... }, { ...Item 1... }, { ...Item 2... }, { ...Item 3... }, { ...Item 4... } ] }, { ...Item 4... }, ] }, { ...Item 3... } ];
Interested to hear of any smart/elegant ideas.
thanks
Richard.
Might not be 'elegant', but at least it works.
-Larry
lstIn = []
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 10, 'LEA_AUTOID': 1000})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2000})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2001})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2003})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3000})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3001})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3002})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3003})
lstIn.append({'COM_AUTOID': 2, 'PRG_AUTOID': 110, 'LEA_AUTOID': 4000})
c="COM_AUTOID"
p="PRG_AUTOID"
l="LEA_AUTOID"
lastc=None
lastp=None
sampleOut=[]
for entry in lstIn:
C=entry[c]
P=entry[p]
L=entry[l]
if C != lastc or P != lastp: sampleOut.append([C,[P,L]])
else: sampleOut[-1:][0][1].append(L)
lastc=C
lastp=P
print "sampleOut=", sampleOut sh*********@gmail.com wrote: Hi - I want to take something like ...
lstIn = [] lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 10, 'LEA_AUTOID': 1000}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2000}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2001}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2003}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3000}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3001}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3002}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3003}) lstIn.append({'COM_AUTOID': 2, 'PRG_AUTOID': 110, 'LEA_AUTOID': 4000})
... and produce something like ...
sampleOut = [[1,[10,1000]],[1,[11,[2000,2001,2003]]],[1,[12,[3000,3001,3002,3003]]],[2,[110,4000]]
lstIn = []
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 10, 'LEA_AUTOID': 1000})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2000})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2001})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2003})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3000})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3001})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3002})
lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3003})
lstIn.append({'COM_AUTOID': 2, 'PRG_AUTOID': 110, 'LEA_AUTOID': 4000})
C="COM_AUTOID"
P="PRG_AUTOID"
L="LEA_AUTOID"
d = {}
# convert data to nested dictionary structure
for item in lstIn:
c, p, l = item[C], item[P], item[L]
if c not in d:
d[c] = dict( {p : [l]} )
else:
if p not in d[c]:
d[c][p] = [l]
else:
d[c][p].append(l)
def dict2list( adict ):
for key in sorted(adict.keys()):
if isinstance( adict[key], dict ):
for item in dict2list(adict[key]):
yield [key] + item
else:
yield [[key] + [adict[key]]]
result = list(dict2list(d))
cigar =
[[1,[10,1000]],[1,[11,[2000,2001,2003]]],[1,[12,[3000,3001,3002,3003]]],[2,[110,4000]]]
no_cigar =
[[1,[10,[1000]]],[1,[11,[2000,2001,2003]]],[1,[12,[3000,3001,3002,3003]]],[2,[110,[4000]]]]
assert result == no_cigar
print
print 'nested dict: ', d
print
print 'result: ', result
nested dict: {1: {10: [1000], 11: [2000, 2001, 2003], 12: [3000, 3001,
3002, 3003]}, 2: {110: [4000]}}
result: [[1, [10, [1000]]], [1, [11, [2000, 2001, 2003]]], [1, [12,
[3000, 3001, 3002, 3003]]], [2, [110, [4000]]]]
Gerard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: rh0dium |
last post by:
Hi All,
While I know there is a zillion ways to do this.. What is the most
efficient ( in terms of lines of code ) do simply do this.
a=1, b=2, c=3 ... z=26
Now if we really want some...
|
by: Der Andere |
last post by:
I need to implement a sorted (ordered) list. STL has no sorted list type as
far as I know. Is there a (straight) way to implement a sorted list using
STL?
BTW: The type of items in the list will...
|
by: prabhat143 |
last post by:
Hi,
Given a singly linked, null terminated list, how can it be converted to
tree? Each node in the list has three attributes: it's ID, it's parent
ID and of course, the next node it's pointing...
|
by: Andy Sutorius |
last post by:
Hi,
Setup/History: Code-behind = C#. I have created an html table with textboxes
in each cell dynamically with asp.net. The number of rows depends on what
the datareader brings back. The columns...
|
by: shrishjain |
last post by:
Hi All,
I need a type where I can store my items in sorted order. And I want to
keep adding items to it, and want it to remain sorted. Is there any
type in .net which I can make use of.
I see...
|
by: comp.lang.tcl |
last post by:
I have a TCL proc that needs to convert what might be a list into a
string to read
consider this:
]; # OUTPUTS Hello World which
is fine for PHP
]; # OUTPUT {{-Hello}}
World, which PHP...
|
by: shellon |
last post by:
Hi all:
I want to convert the float number to sortable integer, like the
function float2rawInt() in java, but I don't know the internal
expression of float, appreciate your help!
|
by: robin1983 |
last post by:
Hello, my last few days, whenever i try to open my local dics (c,d,e) there is alert saying "rose.exe" is not found. please help me to find out the solution......... thanks
|
by: lenygold via DBMonster.com |
last post by:
I am tryieng to convert our time consuming recursive queries too very
efficient queries based
on nested set model.
The only problem is to convert an adjacency list model into a nested set
model,...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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: 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: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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...
| |