[英]Is there a way to find out the total number of changes/edits it takes to convert a list of string values to a second list of string values?
我有两个列表:list1 和 list2(等长),由由字母“a”到“o”组成的相同长度的字符串值组成。
list1= ["gdmnbl","cfakdd","lkbhad",....]
list2= ["gamndl","bfakdd","lkbhad",....]
我需要 output 作为总编辑/更改的列表,需要将值从 list1 转换为 list2。 示例如下所示:
Taking into consideration the first value from list1 and list2 and comparing.
"gdmnbl" and "gamndl"
First character matches, so move on to next character.
Second character not a match, it takes 3 edits/changes to make "d" to "a". EXPLANATION:( "d" to "c", "c" to "b" and "b" to "a").
Third character, fourth character are a match, so ignore.
Fifth character not a match, it takes 2 edits/changes to make "b" to "d". ("b" to c" and then "c" to "d".
Sixth character matches, so move on.
I want the output to be a list consisting of the sum of edits/changes. For the above example it is 3+2= 5.
所以 output 列表应该是这样的:
list3=[5,1,0.....]
有没有办法做到这一点? 非常感谢您的帮助。 谢谢
如果您的意思是对同一 position 的字符之间的距离求和,您可以执行以下操作:
list3=[sum([abs(ord(i)-ord(j)) for i,j in zip(a,b)]) for a,b in zip(list1,list2) ]
list1
和list2
必须具有相同数量的字符串,并且字符串对的长度必须相等。
你可以尝试这样的事情:
def get_diff(s1,s2):
d = 0
s1 = s1.lower()
s2 = s2.lower()
for i in range(len(s1)):
d = d+abs(ord(s1[i])-ord(s2[i]))
return d
list1= ["gdmnbl","cfakdd","lkbhad"]
list2= ["gamndl","bfakdd","lkbhad"]
final_out = []
for i in range(len(list1)):
d = get_diff(list1[i],list2[i])
final_out.append(d)
print(final_out)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.