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 2025 sh*********@gma il.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.appen d([C,[P,L]])
else: sampleOut[-1:][0][1].append(L)
lastc=C
lastp=P
print "sampleOut= ", sampleOut sh*********@gma il.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.ke ys()):
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 bonus points..
a=1, b=2, c=3 ... z=26 aa=27 ab=28 etc..
|
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 be a class. Is it necessary to
implement the > or < operators or to write a compare-function that returns
the larger or smaller of two classes? Ideally, the list begins with the
smallest element.
Cheers,
Matthias
|
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 to. The parent id of root
of the tree is 0. The length of list is not known. What will be the
optimal solution?
Node* convertToTree(Node* listHead);
|
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 (6 of them) are static. After the
table is built I take a snapshot of the names of the textboxes and the
values inside them (before the values of the textboxes are altered by
humans). I need to use this snapshot/data after the webpage has been...
|
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 there is SortedList<key, value> for hash tables, but could find
anything for a sorted list.
Currently I am using List<string> and whenever I add an item, I need to
| |
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 will print literally as {{-Hello}} World, instead of
|
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, with push down stack algorithm to DB2 query. The client does not want
to use Stored Procedure.
Please any Recurcive or CWE ideas.
Thank's in advance.
-- Tree holds the adjacency model
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |