汪晓康

导航

列表对齐

背景

2 个算法模型对字符串进行切割,分成 2 组不同的子字符串,为了对比两个算法模型的区别,需要对这 2 组子字符串进行对齐,使得相同的子字符串的下标相同. 如下图所示
1

源码

from collections import deque

str1 = deque(["A","BC","D","E","FG","H"])
str2 = deque(["A","BC","DE","FG","H"])

newstr1=[]
newstr2=[]
while str1 or str2:
    tmp1 = str1.popleft()
    tmp2 = str2.popleft()
    n1 = len(tmp1)
    n2 = len(tmp2)
    while n1 != n2:
        if n1 > n2:
            # cur2 += 1
            newstr2.append(tmp2)
            newstr1.append("")
            tmp2 = str2.popleft()
            n2 +=len(tmp2)
        else:
            # cur1 +=1
            newstr1.append(tmp1)
            newstr2.append("")
            tmp1 = str1.popleft()
            n1 +=len(tmp1)
    else:
        newstr1.append(tmp1)
        newstr2.append(tmp2)


print(newstr1)
print(newstr2)

posted on 2021-08-09 23:12  汪晓康  阅读(55)  评论(0编辑  收藏  举报