How can I make this sorting algorithm?
modules = {
2:[4], # element 2 must be after el开发者_如何学运维ement 4
1:[],
3:[2,4], # element 3 must be after 2 and 3 elements
4:[1] # element 4 must be after element 1
}
Result [1, 4, 2, 3]
by conditions.
That's called a topological sort. Makefiles do it, and there's a standard UNIX utility called tsort
that will do it.
精彩评论