【问题标题】:How to get the position of a character in Python?如何在 Python 中获取字符的位置?
【发布时间】:2011-01-18 15:58:42
【问题描述】:

如何在 Python 中获取字符在字符串中的位置?

【问题讨论】:

    标签: python string


    【解决方案1】:

    有两种字符串方法,find()index()。两者之间的区别在于找不到搜索字符串时会发生什么。 find() 返回 -1index() 引发 ValueError

    使用find()

    >>> myString = 'Position of a character'
    >>> myString.find('s')
    2
    >>> myString.find('x')
    -1
    

    使用index()

    >>> myString = 'Position of a character'
    >>> myString.index('s')
    2
    >>> myString.index('x')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: substring not found
    

    来自Python manual

    string.find(s, sub[, start[, end]])
    返回 s 中找到子字符串 sub 的最低索引,使得 sub 完全包含在 s[start:end] 中。失败时返回-1startend 的默认值以及负值的解释与切片相同。

    还有:

    string.index(s, sub[, start[, end]])
    find() 类似,但在未找到子字符串时提升ValueError

    【讨论】:

      【解决方案2】:

      为了完整起见,如果需要查找字符串中某个字符的所有位置,可以执行以下操作:

      s = 'shak#spea#e'
      c = '#'
      print([pos for pos, char in enumerate(s) if char == c])
      

      将打印:[4, 9]

      【讨论】:

      • 在 python3 中,出现语法错误 - 应该如何修改?
      • @Sean :打印语句已删除。只保留函数形式。烦人,但答案只是将最后一行更改为:print( [pos for pos, char in enumerate(s) if char == c])
      • foo = ( [pos for pos, char in enumerate(s) if char == c]) 会将坐标 foo 放在列表格式中。我觉得这真的很有帮助
      • 它是 0 索引,0123 而不是 1234,所以实际位置是 5, 10
      【解决方案3】:
      >>> s="mystring"
      >>> s.index("r")
      4
      >>> s.find("r")
      4
      

      “啰嗦”的方式

      >>> for i,c in enumerate(s):
      ...   if "r"==c: print i
      ...
      4
      

      获取子字符串,

      >>> s="mystring"
      >>> s[4:10]
      'ring'
      

      【讨论】:

      • 谢谢告诉我如何根据给定的位置获取字符串的子字符串...
      • @arung: 获取子字符串使用切片:str[from:to] 其中fromto 是索引
      • s.find() 如果未找到子字符串,则返回 -1
      • s.search() 在未找到子字符串时引发 ValueError。如果未找到子字符串,s.find() 返回 -1。
      【解决方案4】:

      为了完成,如果我想在文件名中找到扩展名以便检查它,我需要找到最后一个'.',在这种情况下使用 rfind:

      path = 'toto.titi.tata..xls'
      path.find('.')
      4
      path.rfind('.')
      15
      

      在我的情况下,我使用以下内容,无论完整的文件名是什么:

      filename_without_extension = complete_name[:complete_name.rfind('.')]
      

      【讨论】:

      • 这有助于查找字符串的范围。例如,查找字典可能是:left = q.find("{"); right = q.rfind("}")
      【解决方案5】:

      当字符串包含重复字符时会发生什么? 根据我对index() 的经验,我看到对于重复项,您会返回相同的索引。

      例如:

      s = 'abccde'
      for c in s:
          print('%s, %d' % (c, s.index(c)))
      

      会返回:

      a, 0
      b, 1
      c, 2
      c, 2
      d, 4
      

      在这种情况下,您可以这样做:

      for i, character in enumerate(my_string):
         # i is the position of the character in the string
      

      【讨论】:

      • enumerate 更适合这种事情。
      【解决方案6】:
      string.find(character)  
      string.index(character)  
      

      也许您想看看the documentation 以了解两者之间的区别。

      【讨论】:

      • 来自该链接文档: s.search() 在未找到子字符串时引发 ValueError 。如果未找到子字符串,s.find() 返回 -1。
      【解决方案7】:

      一个字符可能在一个字符串中出现多次。例如在字符串sentence 中,e 的位置是1, 4, 7(因为索引通常从零开始)。但我发现find()index() 这两个函数都返回字符的第一个位置。所以,这可以通过这样做来解决:

      def charposition(string, char):
          pos = [] #list to store positions for each 'char' in 'string'
          for n in range(len(string)):
              if string[n] == char:
                  pos.append(n)
          return pos
      
      s = "sentence"
      print(charposition(s, 'e')) 
      
      #Output: [1, 4, 7]
      

      【讨论】:

        【解决方案8】:

        如果您想找到第一个匹配项。

        Python 有一个内置的字符串方法可以完成这项工作:index()

        string.index(value, start, end)
        

        地点:

        • 值:(必需)要搜索的值。
        • start:(可选)从哪里开始搜索。默认值为 0。
        • end:(可选)在哪里结束搜索。默认是到字符串的末尾。
        def character_index():
            string = "Hello World! This is an example sentence with no meaning."
            match = "i"
            return string.index(match)
                
        print(character_index())
        > 15
        

        如果要查找所有匹配项。

        假设您需要字符 match 所在的所有索引,而不仅仅是第一个索引。

        pythonic 方式是使用enumerate()

        def character_indexes():
            string = "Hello World! This is an example sentence with no meaning."
            match = "i"
        
            indexes_of_match = []
        
            for index, character in enumerate(string):
                if character == match:
                    indexes_of_match.append(index)
            return indexes_of_match
        
        print(character_indexes())
        # [15, 18, 42, 53]
        

        或者更好的列表理解:

        def character_indexes_comprehension():
            string = "Hello World! This is an example sentence with no meaning."
            match = "i"
        
            return [index for index, character in enumerate(string) if character == match]
        
        
        print(character_indexes_comprehension())
        # [15, 18, 42, 53]
        

        【讨论】:

          【解决方案9】:

          more_itertools.locate 是一个第三方工具,用于查找满足条件的项目的所有指标。

          在这里我们找到了字母"i"所有索引位置。

          给定

          import more_itertools as mit
          
          
          text = "supercalifragilisticexpialidocious"
          search = lambda x: x == "i"
          

          代码

          list(mit.locate(text, search))
          # [8, 13, 15, 18, 23, 26, 30]
          

          【讨论】:

            【解决方案10】:

            使用 numpy 快速访问所有索引的解决方案:

            string_array = np.array(list(my_string))
            char_indexes = np.where(string_array == 'C')
            

            【讨论】:

            • 请不要使用这种方法。没有理由将 numpy 带入简单的字符串索引操作。
            • 这取决于,如果您的 csv 文件中有数百万行,您可以完美地使用 Numpy
            【解决方案11】:

            我发现的大多数方法都是指在字符串中查找第一个子字符串。要查找所有子字符串,您需要解决。

            例如:

            定义字符串

            vars = 'iloveyoutosimidaandilikeyou'

            定义子串

            key = 'you'
            

            定义一个函数,可以找到字符串中所有子字符串的位置

            def find_all_loc(vars, key):
            
                pos = []
                start = 0
                end = len(vars)
            
                while True: 
                    loc = vars.find(key, start, end)
                    if  loc is -1:
                        break
                    else:
                        pos.append(loc)
                        start = loc + len(key)
                        
                return pos
            
            pos = find_all_loc(vars, key)
            
            print(pos)
            [5, 24]
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-08-15
              • 2019-12-10
              • 2012-10-22
              • 1970-01-01
              • 1970-01-01
              • 2016-07-05
              • 2021-07-16
              • 2011-09-10
              相关资源
              最近更新 更多