两个字符串之间的匹配字符

2024-04-26 20:34:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我对这段代码一直有意见。我对Python中while循环和缩进的理解似乎还不完整。你知道吗

在下面的代码中,我要比较两个语句。如果第二条(search)语句中的字符在第一条(target)语句中,那么结果应该是“True”。否则,打印“假”。你知道吗

x = "I am a horse."
y = "a r"
  • targetn指目标字符串中的索引
  • searchn是指搜索字符串中的索引
  • letter指目标字符串中的字符
  • word指搜索字符串中的字符
def search_in_string(search, target):
    targetn = 0
    searchn = 0
    while (targetn + 1) != len(target):
        letter = target[targetn]
        word = search[searchn]
        if word == letter:
            targetn = targetn + 1
            searchn = searchn + 1
        if word != letter:        
            targetn = targetn + 1   
    if (searchn + 1) == len(searchn):
        return True
    else: 
        return False

print search_in_string(y, x)

在Python中运行代码时,会出现索引错误,因为searchn超出范围。我将不胜感激:

  1. 代码和代码有什么问题
  2. 我对缩进和while循环的理解是不完整的。你知道吗

Tags: 字符串代码intruetarget目标searchif
3条回答

"1. What's wrong with the code"

第12行输入错误:应该是len(search),而不是len(searchn)。你知道吗

按一个错误关闭:如果索引==len(数组),则它已经离开末尾。删除所有索引测试中的+ 1。你知道吗

第4行的剩余bug:在while条件下测试targetnsearchn。根据字符串中的数据,任何一个都可以先到达末尾。(luk32建议将最终的searchn测试移到循环内部,这也修复了这个bug,但不适用于搜索为空或比目标长的特殊情况。)

"2. Where my understanding of indentations and while loops is incomplete."

不,但可能是列表和数组索引。你知道吗

但更重要的是,函数和变量的命名不是很清楚,代码的样式看起来很像C

命名

在当前代码(修复了错误)中,请尝试以下操作:

  • 将函数名search_in_string更改为is_subsequence
  • 将变量名search更改为partial
  • 将变量名target更改为full
  • 同样地,将索引变量更改为ipartifull或类似值
  • 完全删除变量letterword
  • 在测试中用full[ifull]替换letter,用partial[ipart]替换word

现在不是更清楚了吗?你知道吗

这里我还用一个明显的else替换了互补的第二if测试:

def is_subsequence(partial, full):
    ifull = 0
    ipart = 0
    while ifull != len(full) and ipart != len(partial):
        if partial[ipart] == full[ifull]:
            ifull = ifull + 1
            ipart = ipart + 1
        else:
            ifull = ifull + 1
    if ipart == len(partial):
        return True
    else:
        return False

风格

程序风格看起来仍然像C,带有数组、显式循环和对索引的精细操作。这里有一个机会让您了解一种称为“pythonic”的编码风格。请参阅https://stackoverflow.com/questions/58968/what-defines-pythonian-or-pythonic和这里的几个链接。你知道吗

我想说gnibbler的答案是python式的,但是如果你不懂迭代器和列表理解,可能很难理解。这是另一个有点像python的解决方案,我希望它更容易阅读。但是,您需要了解数组切片。你知道吗

def is_subsequence(partial,full):
    for char in full:
        if partial.startswith(char):
            partial = partial[1:]
    return len(partial) == 0

不需要进入代码的逻辑。

编辑:确定if保护上的监视有错误。 不过,零长度参数也将是一个问题。你知道吗

x = "I am a horse."
y = "a r"

def search_in_string(search, target):
    targetn = 0
    searchn = 0
    while (targetn + 1) != len(target):
        letter = target[targetn]
        word = search[searchn]
        if word == letter:
            targetn = targetn + 1
            searchn = searchn + 1
        if word != letter:        
            targetn = targetn + 1   
    #This will get executed AFTER the WHILE loop
    if (searchn + 1) == len(search):
        return True
    else: 
        return False

print(search_in_string(x,y))

工作代码:

def search_in_string(search, target):
    targetn = 0
    searchn = 0
    while (targetn + 1) != len(target):
        letter = target[targetn]
        word = search[searchn]
        if word == letter:
            targetn = targetn + 1
            searchn = searchn + 1
        if word != letter:        
            targetn = targetn + 1   
        #This will get executed WITHIN the WHILE loop
        if (searchn + 1) == len(search):
            return True
        else: 
            return False

我认为有一个更简单的方法来编写你的函数

def search_in_string(search, target):
    iter_t = iter(target)
    return all(c in iter_t for c in search)

例如:

>>> search_in_string("a r", "I am a horse.")
True
>>> search_in_string("a re", "I am a horse.")
True
>>> search_in_string("a er", "I am a horse.")
False

相关问题 更多 >