简体   繁体   English

需要帮助了解为什么我的find()方法的使用没有正确执行

[英]Need Help Understanding Why My Usage of the find() Method Isn't Executing Correctly

Write a function called string_finder. 编写一个名为string_finder的函数。 string_finder should take two parameters: a target string and a search string. string_finder应该有两个参数:目标字符串和搜索字符串。 The function will look for the search string within the target string. 该函数将在目标字符串中查找搜索字符串。

The function should return a string representing where in the target string the search string was found: 该函数应返回一个字符串,表示搜索字符串在目标字符串中的位置:

If search string is at the very beginning of target string, then return "Beginning". 如果搜索字符串位于目标字符串的最开头,则返回“Beginning”。 For example: 例如:

string_finder("Boston Common", "Boston") -> "Beginning"

If search string is at the very end of target string, then return "End". 如果搜索字符串位于目标字符串的最后,则返回“End”。 For example: 例如:

string_finder("Boston Common", "Common") -> "End"

If search string is in target string but not at the very beginning or very end, then return "Middle. For example: 如果搜索字符串在目标字符串中但不在开头或结尾,则返回“中间。例如:

string_finder("Boston Common", "ton") -> "Middle"

If search string is not in target string at all, then return "Not found". 如果搜索字符串根本不在目标字符串中,则返回“未找到”。 For example: 例如:

string_finder("Boston Common", "Idaho") -> "Not found"

Assume that we're only interested in the first instance of the search string if it appears multiple times in the target string, and that search string is definitely shorter than target string. 假设我们只对搜索字符串的第一个实例感兴趣,如果它在目标字符串中出现多次,并且该搜索字符串肯定比目标字符串短。

I am able to write a function that utilizes conditionals (if, elif, and else) and the find() method in Python 3. In some of my conditionals with the find() method, I'm using all three parameters for the find() method, which are string, starting index and ending index. 我能够在Python 3中编写一个利用条件(if,elif和else)和find()方法的函数。在一些使用find()方法的条件中,我使用了所有三个参数进行查找()方法,它是字符串,起始索引和结束索引。

def string_finder(target_string, search_string):
    if search_string.find(target_string, 0, len(search_string)):
        return "Beginning"
    elif search_string.find(target_string, -1, -(len(target_string) - len(search_string))):
        return "End"
    elif search_string.find(target_string):
        return "Middle"
    else:
        return "Not found"

Below are some lines of code that will test your function. 下面是一些将测试您的功能的代码行。 You can change the value of the variable(s) to test your function with different inputs. 您可以更改变量的值以使用不同的输入测试您的函数。

If your function works correctly, this will originally print: Start, Middle, End, Not found, each on their own line. 如果你的功能正常,这将最初打印:开始,中间,结束,未找到,每个都在他们自己的行上。

print(string_finder("Boston Common", "Boston"))
print(string_finder("Boston Common", "ton"))
print(string_finder("Boston Common", "Common"))
print(string_finder("Boston Common", "Idaho"))

which should print 哪个应该打印

Beginning
Middle
End
Not found

However, with my current code I get: 但是,根据我目前的代码,我得到:

Beginning
Beginning
Beginning
Beginning

I can't figure out how I messed up my conditionals and how to tweak. 我无法弄清楚我是如何弄乱我的条件以及如何调整的。 Also, I'm open to any better ways of approaching this problem. 此外,我愿意接受任何更好的方法来解决这个问题。

First of all, your second if should be "elif". 首先,你的第二个应该是“elif”。 It works if u do it like this: 它是有效的,如果你这样做:

def string_finder(target_string, search_string):
    if search_string.find(target_string, 0, len(search_string)):
        print("Beginning")
    elif search_string.find(target_string, -1, -(len(target_string) - len(search_string))):
        print("End")
    elif search_string.find(target_string):
        print("Middle")
    else:
        print("Not found")
string_finder("Boston Common", "ton")

The find function ( docs here ) returns -1 when it does not find the substring (and returns the lowest indice where it found the string), and -1 is interpreted as True by python ( docs for truth/false in python here ), therefore the first if will always evaluate to true. find函数( 此处为docs )在找不到子字符串时返回-1(并返回找到字符串的最低indice),而python将此字符解释为True此处为python中的true / false文档 ),因此,第一个if将始终评估为true。

You should check if the value returned is different from -1 . 您应该检查返回的值是否与-1不同。 Or if you want, you can check if the value return to tell where python found the string: 或者如果你想,你可以检查值是否返回告诉python在哪里找到了字符串:

def string_finder(target_string, search_string):
    start_index = search_string.find(target_string)
    end_index = start_index + len(target_string)
    if start_index == -1: # Not found, find returns -1
      return "Not found"
    elif start_index == 0:
      return "Beginning"
    elif end_index == len(search_string):
      return "End"
    else:
      return "Middle"

Let me explain the code idea: 让我解释代码的想法:

As the docs for find says, it "Return the lowest index in the string where substring sub is found". 正如find的文档所说,它“返回找到substring sub的字符串中的最低索引”。 So, start_index represents where in the search_string the target_string starts. 因此, start_index表示target_stringsearch_string的起始位置。 The end_index then represents where in the search_string the target_string ends. 然后end_index表示target_string结束的search_string的位置。 So, if you have start_index as 0 (first position), it is in the beginning of the string. 因此,如果您将start_index为0(第一个位置),则它位于字符串的开头。 If you have end_index equals the len(target_string) it goes until the end of the search_string . 如果你有end_index等于len(target_string)它会一直到search_string结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM