Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何显示字符串对齐?_Python - Fatal编程技术网

Python 如何显示字符串对齐?

Python 如何显示字符串对齐?,python,Python,我几乎完成了这个程序,我只是有点被最后一部分卡住了。 运行时,程序将要求输入两个字符串。然后比较这些值以查看最小编辑距离。删除和插入的成本为1,替换(删除和插入)的成本为2 因为最后两个字母需要替换才能从一个字母到另一个字母 我遇到的问题是显示对齐。我想表现得像: q u i c k l y s s q u i c k e r 显示两个替换以及它们的位置。这也适用于删除和插入 到目前为止: #!/usr/bin/env python import sys from sys

我几乎完成了这个程序,我只是有点被最后一部分卡住了。 运行时,程序将要求输入两个字符串。然后比较这些值以查看最小编辑距离。删除和插入的成本为1,替换(删除和插入)的成本为2

因为最后两个字母需要替换才能从一个字母到另一个字母

我遇到的问题是显示对齐。我想表现得像:

q u i c k l y
          s s
q u i c k e r
显示两个替换以及它们的位置。这也适用于删除和插入

到目前为止:

#!/usr/bin/env python
import sys
from sys import stdout

def  Edit_Distance(target, source):


n = len(target)
m = len(source)

distance = [[0 for i in range(m+1)] for j in range(n+1)]

for i in range(1,n+1):
    distance[i][0] = distance[i-1][0] + insertCost(target[i-1])

for j in range(1,m+1):
    distance[0][j] = distance[0][j-1] + deleteCost(source[j-1])

for i in range(1,n+1):
    for j in range(1,m+1):
       distance[i][j] = min(distance[i-1][j]+1,
                            distance[i][j-1]+1,
                            distance[i-1][j-1]+substCost(source[j-1],target[i-1]))
return distance[n][m]

def substCost(x,y):
    if x == y:
        return 0
    else:
        return 2

def insertCost(x):
    return 1

def deleteCost(x):
    return 1


# User inputs the strings for comparison
word1 = raw_input("Enter A Word: ")
word2 = raw_input("Enter The Second Word: ")

# Simple conditional that will set the length of the range loop below based on the longest string
if (word2 >= word1):
    x = len(word2)
else:
        x = len(word1)

# x is then the longest string length so that we have the perfect length range loop
# stdout.write allows us to print multiple things on the same line, instead of tabbing down a line each time
print ("The minimum edit distance between S1 and S2 is: ", Edit_Distance(word1,word2))

print list(word1)
for i in range(x):
    if(word1[i] != word2[i]):
        print("D")

print list(word2)

如果您的问题只是如何打印对齐的“D”,您应该查看

python文档的第页,在这里您可以找到如何格式化字符串(如果我理解的话,这就是您所要求的)

顺便说一句,只是关于如何打印对齐的提示(考虑到代码工作良好):为什么不使用列表来存储循环中的“D”