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

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)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.