Find all the occurrences of a character in a string

后端 未结 7 612
情深已故
情深已故 2020-12-01 12:02

I am trying to find all the occurences of \"|\" in a string.

def findSectionOffsets(text):
    startingPos = 0
    endPos = len(text)

    for position in te         


        
7条回答
  •  长情又很酷
    2020-12-01 12:29

    if you want index of all occurance of | character in a string you can do this

    In [30]: import re
    
    In [31]: str = "aaaaaa|bbbbbb|ccccc|ffffdd"
    
    In [32]: [x.start() for x in re.finditer('\|', str)]
    Out[32]: [6, 13, 19]
    

    also you can do

    In [33]: [x for x, v in enumerate(str) if v == '|']
    Out[33]: [6, 13, 19]
    

提交回复
热议问题