【问题标题】:Python: Find a substring in a string and returning the index of the substringPython:在字符串中查找子字符串并返回子字符串的索引
【发布时间】:2014-03-17 13:32:01
【问题描述】:

我有:

  • 一个函数:def find_str(s, char)

  • 还有一个字符串:"Happy Birthday",

我基本上想输入"py" 并返回3,但我不断得到2 来代替返回。

代码:

def find_str(s, char):
    index = 0           
    if char in s:
        char = char[0]
        for ch in s:
            if ch in s:
                index += 1
            if ch == char:
                return index

    else:
        return -1

print(find_str("Happy birthday", "py"))

不知道怎么回事!

【问题讨论】:

  • 我不确定这段代码有什么问题。你用char = char[0] 激怒了你的第二个论点。之后你的第二个参数只是“p”,它在位置 2 (H,a,p) 找到匹配项,就是这样。

标签: python string indexing substring


【解决方案1】:

字符串对象上有一个内置方法find

s = "Happy Birthday"
s2 = "py"

print(s.find(s2))

Python 是一种“包含电池的语言”,编写的代码可以完成您想要的大部分工作(无论您想要什么).. 除非这是家庭作业 :)

find 如果找不到字符串,则返回 -1。

【讨论】:

  • @Kev1n91 不被接受真是奇怪。
  • 我在原始答案被接受后回答了这个问题。我只是偶然发现它,并认为我会增加我的两美分的价值。
  • 没有被接受,因为@Tyler 不能使用 str.find 或 str.index
【解决方案2】:

理想情况下,您应该使用 str.findstr.index,就像痴呆刺猬所说的那样。但是你说你不能……

您的问题是您的代码仅搜索搜索字符串的第一个字符(第一个字符)位于索引 2。

您基本上是说如果char[0]s 中,则递增index 直到ch == char[0] 在我测试它时返回3,但它仍然是错误的。这是一种方法。

def find_str(s, char):
    index = 0

    if char in s:
        c = char[0]
        for ch in s:
            if ch == c:
                if s[index:index+len(char)] == char:
                    return index

            index += 1

    return -1

print(find_str("Happy birthday", "py"))
print(find_str("Happy birthday", "rth"))
print(find_str("Happy birthday", "rh"))

它产生了以下输出:

3
8
-1

【讨论】:

  • 我需要编写一个执行 .find 函数的算法。不幸的是,我不能只使用它!
  • @Tyler 编辑了我的答案,因为您的问题没有实际答案。
  • 有一个更好的方法,就像@dementedhedgehog 所建议的那样。这应该是公认的答案
  • @Kev1n91 我知道,我在回答中说过,但 OP 评论他不能使用它。
【解决方案3】:

regular expression 中还有另一个选项,search 方法

import re

string = 'Happy Birthday'
pattern = 'py'
print(re.search(pattern, string).span()) ## this prints starting and end indices
print(re.search(pattern, string).span()[0]) ## this does what you wanted

顺便说一句,如果你想找到一个模式的所有出现,而不仅仅是第一个,你可以使用finditermethod

import re

string = 'i think that that that that student wrote there is not that right'
pattern = 'that'

print([match.start() for match in re.finditer(pattern, string)])

这将打印比赛的所有开始位置。

【讨论】:

    【解决方案4】:

    在使用find() 时添加到@demented 刺猬答案

    效率方面

    在调用find()之前,先检查一下 s1 是否在 s2 中可能是值得的。
    如果您知道大多数时候 s1 不会是 s2 的子字符串,这会更有效

    由于in 运算符非常高效

     s1 in s2
    

    转换效率会更高:

    index = s2.find(s1)
    

    index = -1
    if s1 in s2:
       index = s2.find(s1)
    

    这对于find() 将返回大量 -1 非常有用。

    由于在我的算法中多次调用find(),所以我发现它的速度要快得多,所以我认为值得一提

    【讨论】:

      【解决方案5】:

      这是一个简单的方法:

      my_string = 'abcdefg'
      print(text.find('def'))
      

      输出:

      3

      如果子字符串不存在,你会得到-1。 例如:

      my_string = 'abcdefg'
      print(text.find('xyz'))
      

      输出:

      -1

      有时,如果子字符串不存在,您可能想抛出异常:

      my_string = 'abcdefg'
      print(text.index('xyz')) # It returns an index only if it's present
      

      输出:

      回溯(最近一次通话最后一次):

      文件“test.py”,第 6 行,在 打印(text.index('xyz'))

      ValueError:找不到子字符串

      【讨论】:

        【解决方案6】:

        派对迟到了,正在搜索相同的内容,因为“in”无效,我刚刚创建了以下内容。

        def find_str(full, sub):
            index = 0
            sub_index = 0
            position = -1
            for ch_i,ch_f in enumerate(full) :
                if ch_f.lower() != sub[sub_index].lower():
                    position = -1
                    sub_index = 0
                if ch_f.lower() == sub[sub_index].lower():
                    if sub_index == 0 :
                        position = ch_i
        
                    if (len(sub) - 1) <= sub_index :
                        break
                    else:
                        sub_index += 1
        
            return position
        
        print(find_str("Happy birthday", "py"))
        print(find_str("Happy birthday", "rth"))
        print(find_str("Happy birthday", "rh"))
        

        产生

        3
        8
        -1
        

        如果不需要不区分大小写的查找,请删除 lower()。

        【讨论】:

          【解决方案7】:

          没有直接回答这个问题,但我最近收到了一个类似的问题,要求我计算给定字符串中子字符串重复的次数。这是我写的函数:

          def count_substring(string, sub_string):
              cnt = 0
              len_ss = len(sub_string)
              for i in range(len(string) - len_ss + 1):
                  if string[i:i+len_ss] == sub_string:
                      cnt += 1
              return cnt
          

          find() 函数可能只返回第一次出现的索引。存储索引而不是计数,可以为我们提供子字符串在字符串中重复的一组不同的索引。

          免责声明:我对 Python 编程“非常”陌生。

          【讨论】:

            猜你喜欢
            • 2021-12-18
            • 1970-01-01
            • 2021-08-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-04
            • 1970-01-01
            相关资源
            最近更新 更多