[英]Mismatched strings in Python — how to continue process?
我是编程的初学者,正在尝试弄清一些Python作业,并觉得自己太过亲密了! 我在这个论坛上查询了很多问题,但找不到相关的答案,所以去了。
问题:编写一个名为find_mismatch的函数,该函数接受两个字符串作为输入参数并返回:
如果两个字符串完全匹配,则为0。
如果两个字符串的长度相同且仅一个字符不匹配,则为1。
如果两个字符串的长度相同或两个或多个字符不匹配,则为2。
大写字母与小写字母相同。
我的主要问题是我的程序仅在一次不匹配后似乎结束。 确保其贯穿整个代码的一种好方法是什么? 我认为它也可能比需要的更加杂乱。
def find_mismatch(s1,s2):
s1=s1.lower()
s2=s2.lower()
x=s1.split()
y=s2.split()
mismatch=0
count=0
for char in (0,len(x+1)):
if len(x)==len(y) and x[0:]==y[0:]:
return 0
if not len(s1)==len(s2):
return 2
#if len(x)==len(y) and not x[0:]==y[0:]:#problem is here?
if len(s1)==len(s2) and not x[count]==y[count]:
mismatch=mismatch+1
#count=count+1#problem here
if mismatch<2:
return 1
if mismatch>=2:
return 2
我倾向于使这些问题变得过于复杂-非常感谢您的帮助! 如果添加注释中提到的计数器,则会出现索引错误。
请在下面找到我的答案,希望对您有所帮助。
def find_mismatch(s1,s2):
count = 0
if len(s1) == len(s2):
for i in range(len(s1)):
if s1[i].lower() != s2[i].lower():
count += 1
if count == 0:
return 0
elif count == 1:
return 1
else:
return 2
else:
return 2
确保其贯穿整个代码的一种好方法是什么?
不要在循环内执行return
语句,除非您想尽早退出循环。 我没有测试其余的功能,但是您可以执行以下操作:
def find_mismatch(s1,s2):
s1=s1.lower()
s2=s2.lower()
x=s1.split()
y=s2.split()
mismatch=0
count=0
for char in range(0,len(x+1)):
if len(x)==len(y) and x[0:]==y[0:]:
return 0
if not len(s1)==len(s2):
return 2
#if len(x)==len(y) and not x[0:]==y[0:]:
if len(s1)==len(s2) and not x[count]==y[count]:
mismatch +=1
#count +=1
return mismatch if mismatch <=2 else 2
例如,由于字符串是可迭代的,因此您的代码可以进一步简化,我怀疑您需要将其转换为x = s1.split()
等的列表。
def mismatch(s1, s2):
s1, s2 = s1.lower(), s2.lower()
if len(s1) != len(s2):
return 2
elif s1 == s2:
return 0
else:
# same length, zip the iterable strings and check for mismatch:
mismatch = [itm for itm in zip(s1,s2) if itm[0]!=itm[1]]
# print mismatch
return 1 if len(mismatch)==1 else 2
以下程序可以满足您的需求。 第一个find_mismatch
函数被第一个阴影find_mismatch
,不需要并且不会运行。 它只是具有更明确的参数和变量名称的函数版本。 我建议使用这两个功能中的第二个功能,因为它更易于阅读,并提供有关其功能的文档。 随意修改代码以适应您的使用情况!
#! /usr/bin/env python3
def main():
print(find_mismatch('Hello, world!', 'Hello, world'))
print(find_mismatch('Hello, world!', 'Hello, world!'))
print(find_mismatch('Hello, world!', 'Hallu, world!'))
print(find_mismatch('Hello, world!', 'Hello, world.'))
def find_mismatch(string_a, string_b):
if len(string_a) != len(string_b):
return 2
folded_a, folded_b = string_a.casefold(), string_b.casefold()
if folded_a == folded_b:
return 0
found_difference = False
for character_a, character_b in zip(folded_a, folded_b):
if character_a != character_b:
if found_difference:
return 2
found_difference = True
return 1
def find_mismatch(a, b):
"""Find mismatches in strings a and b.
Returns 0 when strings are the same.
Returns 1 when strings differ in one character.
Returns 2 any other time (different lengths or more mismatches)."""
if len(a) != len(b):
return 2
a, b = a.casefold(), b.casefold()
if a == b:
return 0
error = False
for a, b in zip(a, b):
if a != b:
if error:
return 2
error = True
return 1
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.