Python 从一个整数列表到另一个整数列表的最小转换次数

Python 从一个整数列表到另一个整数列表的最小转换次数,python,dynamic-programming,Python,Dynamic Programming,给定序列p和T的列表,我正试图写一个算法 (函数minNumOfTransformations(P,T))返回从P到T所需的最小移动或转换次数。这些转换包括替换、插入或删除。例如,从[0,1,2,3,5]到[0,2,4,5]至少需要两个变换;加入1而以3取代4。我正试图通过python上的动态编程来实现这一点 def minNumOfTransformations(P, T): # If first list is empty, the only option is to # insert

给定序列p和T的列表,我正试图写一个算法 (函数minNumOfTransformations(P,T))返回从P到T所需的最小移动或转换次数。这些转换包括替换、插入或删除。例如,从[0,1,2,3,5]到[0,2,4,5]至少需要两个变换;加入1而以3取代4。我正试图通过python上的动态编程来实现这一点

def minNumOfTransformations(P, T):


# If first list is empty, the only option is to 
# insert all the elements
if m==0: 
     return n 

# If second list is empty, the only option is to 
# remove all the characters of the first list 
if n==0: 
    return m 

# approach here is to solve simpler sub problems but this is where I get stuck 
if P[m-1]==T[n-1]: 
    return minNumofTransformations(P[m-1], T[n-1]) 

阻止你在谷歌上找到现成答案的原因可能是你不知道你要找的是Levenshtein距离,这是序列之间差异的标准度量

有一个专门为实现这一点而构建并用C实现的程序,因此它可能比您编写的任何程序都要快

如果您真的想自己在Python中实现这一点,这将有所帮助:

那么,您的问题是什么?请提供所有代码。m和n都是在你的问题之外定义的(大概是?),谢谢。对于动态规划方法,谷歌称其时间复杂度为O(nm)。你知道为什么吗?