-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.py
37 lines (30 loc) · 1.05 KB
/
sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python
#coding=utf-8
'''
@author: stonelee
@email: [email protected]
@blog: http://stonelee.info
@description: sort based on requires
调用方法: sort_by_require(data), 其中data为表示依赖关系的dict
返回按照依赖关系排列的数组
'''
def sort_by_require(data):
def sort(key,data):
if key not in results:
for value in data[key]:
if value not in results:
if value in data.keys():
sort(value,data)
else:
results.append(value)
results.append(key)
results = []
for key in data:
sort(key,data)
return results
if __name__ == '__main__':
requires = {'a':['b','c'],'c':['d','e'],'f':['a','c']}
assert sort_by_require(requires) == ['b', 'd', 'e', 'c', 'a', 'f']
requires = {'h':[],'a':['b','c'],'c':['d','e'],'f':['a','c'],'g':[]}
#如果没有依赖关系则没有固定次序
assert sort_by_require(requires) == ['b', 'd', 'e', 'c', 'a', 'h', 'g', 'f']