我有以下代码递归调用 internal_dep 中的每个值,有没有办法可以将函数调用更改为仅调用 internal_dep 中存在的键或跳过对跳过列表中的键的调用?
internal_dep = {'313115': ['313113'], '313117': ['313115'], '313137': ['313136'], '313136': ['313119'], '313119': ['313118'], '313118': ['313117']}
mainlist = ['313137', '317152' , '314449']
skip = ['313113']
def _getdep(item):
local_list, temp_list = [], []
temp_list.extend(internal_dep[item])
local_list.extend(temp_list)
for new_item in temp_list:
local_list.extend(_getdep(new_item))
return local_list
build_dep_list = []
for item in mainlist:
build_dep_list.append(item)
build_dep_list.extend(_getdep(item))
print build_dep_list
错误:-
Traceback (most recent call last):
File "test.py", line 16, in <module>
build_dep_list.extend(_getdep(item))
File "test.py", line 10, in _getdep
local_list.extend(_getdep(new_item))
File "test.py", line 10, in _getdep
local_list.extend(_getdep(new_item))
File "test.py", line 10, in _getdep
local_list.extend(_getdep(new_item))
File "test.py", line 10, in _getdep
local_list.extend(_getdep(new_item))
File "test.py", line 10, in _getdep
local_list.extend(_getdep(new_item))
File "test.py", line 10, in _getdep
local_list.extend(_getdep(new_item))
File "test.py", line 6, in _getdep
temp_list.extend(internal_dep[item])
KeyError: '313113'
EXPECTED OUTPUT:-
['313115', '313113', '313117','313137','313136','313119','313118','313117']