我正在寻找可以实时进行的流程,因为它可以被概括并且也很有效。
这是我尝试的代码。它具有基本逻辑。但是,这不适用于所有输入。
result=[{4:5},{4:6},{4:7}]
new=[]
new=result.copy()
print("Original",result)
for i in range(0,len(result)-1):
for key, value in result[i].items():
for j in range(1,len(result)):
for key_1, value_1 in result[j].items() :
if i!=j:
if key==key_1:
print("when key=key\n",result[i],"=",result[j])
dict={value:value_1}
new[j]=dict
print(new)
break;
if key==value_1:
print("when key=value\n",result[i],"=",result[j])
dict={value:key_1}
new[j]=dict
print(new)
if value==value_1:
print("when value=value\n",result[i],"=",result[j])
dict={key:key_1}
new[j]=dict
print(new)
else:
break;
print("\nFinal =",new)
print("\nCorrect =[{4:5},{5:6},{6:7}]" )
输入:[{4:5},{4:6},{4:7}] 输出:[{4:5},{5:6},{6:7}]