0

Hello Folks,

I have a data structure that is represented like this

{   1: {   'A1': [   
					3,
						[   
						'Val1'
						]
					],
           'A2': [
					9,
						[
						'Val2',
						'Val3'
						]
		}
	2: {   'A3': [
					2,
						[
							'Val4'
						]
					],
           'A4': [
					1,
						[
							'Val5',
							'Val6'
						]
					]
		},

#etc....
}

How would i be able to retrive the value like this
A2 > A1 > A3 > A4 ;
i do not hae any idea how i can sort on a child like this ?

Any help would be appreciated

Thanks

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Gribouillis
0

You can write first a generator for the child elements that you want to sort

def child_elements(the_structure):
    for number, subdict in the_structure.iteritems():
        for A_name, sublist in subdict.iteritems():
            yield (A_name, sublist)

Then you define a score function for the extracted items and you sort the sequence according to this score function

def score((A_name, sublist)):
    return sublist[0]

# assuming 'my_structure' is the variable name which contains your structure
result_list = sorted(child_elements(my_structure), key=score, reverse=True)
print(result_list)

Edited by Gribouillis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.