我的代码遇到问题。这只是部分工作。
我的目标是在字典中将键(我的文件)与值(顶级父级)相关联。
因此,对于FILE 2,3,4&5,我想将值“ FILE2”关联 但是现在,键“ FILE5”的值为“ FILE4”,而不是“ FILE2”。
{'FILE': 'FILE', 'FILE2': 'FILE2', 'FILE8': 'FILE8', 'FILE9': 'FILE9', 'FILE2': 'FILE12', 'FILE3': 'FILE2', 'FILE10': 'FILE9', 'FILE13': 'FILE12', 'FILE4': 'FILE2', 'FILE1': 'FILE9', 'FILE5': 'FILE4'}
我认为我缺少递归功能。但我不知道如何解决。
def FunctionD(item, dico):
'''Create a dictionary where UltimateParent is key and value'''
dico[item] = item
def FunctionE(child, parent, dico,dico2):
'''Create a dictionary to assign the UltimateParent to some Children. If parent is key, we assign the parent as a value.'''
'''/!\Here is the problem I think'''
if parent in dico2 :
dico[child] = parent
if parent in dico2.values():
dico[child] = dico[parent]
def FunctionA(path_src, path_dest, path_excel) :
''' Main fonction'''
flag = input('Do you want the files sort by UltimateParent, by Reviewer or by Responsiveness ? (any key/Rw/Rp)')
'''Read the Excel sheet and store it into a dataframe'''
excel_file =path_excel
df = pd.read_excel(excel_file)
A = df.iloc[:,0].tolist() #ColumnA
B = df.iloc[:,1].tolist() #ColumnB
'''Try of assign the correct Ultimate Parent for each File'''
dico ={}
[FunctionD(i,dico) for i,j in zip(A,B) if isinstance(j, float)] #Dic Parents
dico2 = dico.copy()
[FunctionE(i, j, dico,dico2) for i, j in zip(A, B) if not isinstance(j, float)] #Associe parents et enfants (sf petitsenfants)
dico3 =dico.copy()
[FunctionE(i, j, dico, dico3) for i, j in zip(A, B) if not isinstance(j, float)] # Associe enfant aux parents
dico4 = dico.copy()
[FunctionE(i, j, dico, dico4) for i, j in zip(A, B) if not isinstance(j, float)]
'''Creation of the UltimateParent folder'''
[FunctionB(path_dest, i) for i,j in zip(A,B) if isinstance(j, float)] #creation des dossiers des parents