我有一些代码来找出字符串之间的差异。目前,它适用于相同长度的字符串,但我正试图让它适用于不同长度的字符串。我该怎么做
我添加了一个新变量longest_seq
来尝试解决这个问题,但我不知道如何使用它。你知道吗
ref_seq = "pandabears"
map_seq = "pondabear"
longest_seq = map_seq
if len(ref_seq) > len(map_seq):
longest_seq == ref_seq
for i in range(len(longest_seq)):
if ref_seq[i] != map_seq[i]:
print i, ref_seq[i], map_seq[i]
像这样的东西应该能奏效。你知道吗
将打印:
对于Python2,可以使用
itertools.izip
来实现:输出:
在Python 3中,可以使用内置的
zip
函数:zip
存在于Python 2中,但建议使用itertools.izip
,因为它在需要时生成元组(在每次迭代中都生成一个新元组),而不是一次生成所有元组,在Python 3中,zip
执行Python 2中itertools.izip
所执行的操作。你知道吗相关问题 更多 >
编程相关推荐