我有以下代碼遞歸調用internal_dep中的每個值,有沒有一種方法可以更改函數調用以僅調用鍵存在於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']
你的意思是隻調用'mainlist'中的那些跳過'skip'列表中的鍵? – 2013-04-28 09:19:35
@AshwiniChaudhary - 是的,調用mainlist及其依賴項中的那些,只有internal_dep – user2125827 2013-04-28 09:48:34
中提到的鍵請參閱我編輯的解決方案。 – 2013-04-28 10:12:44