检查字符串中的参数是否后跟特定字母 – Python

huangapple go评论75阅读模式
英文:

Check if parameter in string is followed by a specific letter - python

问题

我发现了一个类似的答案,但只是用Java。以下是我目前的代码示例:

def find_x(phrase, target):
  i = phrase.find(target)
  if 'x' in phrase[i + 1]:
    return True
  else:
    return False

后来我意识到,如果短语是'Texas',目标是'Te',那么find函数只会给出目标的第一个出现的第一个字母的索引。因此,当它检查下一个字母是否是'x'时,它将被视为目标的一部分,返回False而不是True。请告诉我是否有解决这个问题的方法,谢谢!

英文:

I found a similar answer for this but only in java. Here is an example of my code at the moment:

def find_x(phrase, target):
  i=phrase.find(target)
  if 'x' in phrase[i+1]:
    return True
  else:
    return False

I then realized in the case the phrase is 'Texas' and the target is 'Te' the find function will only give the index of the first letter of the first occurrence of the target. Therefore when it checks if the next letter is 'x' it will be part of the target returning False instead of True. Let me know if there is a way to get around this issue, thanks!

答案1

得分: 0

根据评论中的建议检查 target + 'x' in phrase,您可以执行此检查,并且还可以检查它是否位于第一次出现 target 的位置相同:

def find_x(phrase, target):
    return target + 'x' in phrase and phrase.find(target) == phrase.find(target + 'x')

find_x('Texas', 'Te')
True
find_x('TeTexas', 'Te')
False

明确查找 target 的第一次出现,然后检查其紧接其后的字符的选项更有效,因为它只需要一次扫描 phrase。 您只需捕获潜在的 IndexErrorValueError,然后返回 False 而不是引发异常。

def find_x(phrase, target):
    try:
        return phrase[phrase.index(target) + len(target)] == 'x'
    except (IndexError, ValueError):
        return False

find_x('Texas', 'Te')
True
find_x('TeTexas', 'Te')
False
find_x('Te', 'Te')  # 这是会引发 IndexError 的情况
False
find_x('Texas', 'Tee')  # 这是会引发 ValueError 的情况
False
英文:

Building on the suggestion in the comments to check for target + 'x' in phrase, you could just make that check and then also check that it's in the same spot as the first occurrence of target:

>>> def find_x(phrase, target):
...     return target + 'x' in phrase and phrase.find(target) == phrase.find(target + 'x')
...
>>> find_x('Texas', 'Te')
True
>>> find_x('TeTexas', 'Te')
False

The option of explicitly looking for the first occurrence of target and then checking the character immediately after it is more efficient, since it only requires one scan through phrase. You just want to catch the potential IndexError or ValueError and return False instead of raising.

>>> def find_x(phrase, target):
...     try:
...         return phrase[phrase.index(target) + len(target)] == 'x'
...     except (IndexError, ValueError):
...         return False
...
>>> find_x('Texas', 'Te')
True
>>> find_x('TeTexas', 'Te')
False
>>> find_x('Te', 'Te')  # this is the case that'll raise IndexError
False
>> find_x('Texas', 'Tee')  # this is the case that'll raise ValueError
False

huangapple
  • 本文由 发表于 2023年3月7日 11:10:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定