简体   繁体   English

如何获得字符串中重复出现的字符的位置?

[英]How can I get the position of a recurring character in a string?

I have this string: 我有这个字符串:

|C100|0|1|F120570|55|00|32|754929|35150456228356008116550320007549291371271334|17042015|20042015|2077,14|1|2062,48|0,00|3729,51|9|0,00|0,00|3,51|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00| | C100 | 0 | 1 | F120570 | 55 | 00 | 32 | 754929 | 35150456228356008116550320007549291371271334 | 17042015 | 20042015 | 2077,14 | 1 | 2062,48 | 0,00 | 3729,51 | 9 | 0,00 | 0,00 | 3,51 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 |

And I need to find the index / position of the 23rd "|" 而且我需要找到第23个“ |”的index / position using Python. 使用Python。

Since I have more than one "|", I don't know how to do that.. 由于我有多个“ |”,因此我不知道该怎么做。

I've found this code to find the second occurrence 我已经找到此代码来查找第二次出现

def get_second_index(input_string, sub_string):
    return input_string.index(sub_string, input_string.index(sub_string) + 1)

but this one isn't helping me with my problem. 但这并不能帮助我解决我的问题。

I thought that maybe I can get that with a loop like while x < 23: but I can't figure it out exactly how... 我以为也许可以用while x < 23:那样的循环来得到它,但是我不知道到底是怎么回事...

The easiest I can think of is to loop through each character and keep the count of how many '|' 我能想到的最简单的方法是遍历每个字符并保持计数多少个'|' you have found. 你已经找到了。

count = 0
for index, c in enumerate(string):
   if c == '|':
      count = count + 1
   if count == 23:
      print index
      break

You could use the following regular expression to match exactly 23 '|' 您可以使用以下正则表达式精确匹配23个'|' characters: 字符:

text = "|C100|0|1|F120570|55|00|32|754929|35150456228356008116550320007549291371271334|17042015|20042015|2077,14|1|2062,48|0,00|3729,51|9|0,00|0,00|3,51|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|"

print re.match("(\|.*?){23}", text).end() - 1

This gives 154 for your example text. 这将为您的示例文本提供154。

x = 0
for index, char in enumerate(string):
    if char == "|" and x == 23:
        output_index = index
    elif char == "|":
        x += 1
print output_index

enumerate() returns the index and value of each element in a string, list, dict, etc. enumerate()返回字符串,列表,字典等中每个元素的索引和值。

You can use re.finditer to get a generator contain all groups contain | 您可以使用re.finditer获取包含所有组的生成器| the use re.matchObject.start() method which return the start position of your match and get the 23th element with enumerate and a generator expression in next function : 使用re.matchObject.start()方法返回比赛的开始位置,并next函数中获取带有enumerate和生成器表达式的第23个元素:

>>> all_case=re.finditer(r'\|',s)
>>> next(j.start() for i,j in enumerate(all_case,1) if i==23)
154

You can use string.find() within a loop and adjust the start index as you go. 您可以在循环中使用string.find()并随时调整起始索引。 Something like the following: 类似于以下内容:

start = -1
end = len(yourstring)
x = 0
while x < 23:
  start = string.find(yourstring, "|", start + 1, end)\
  x +=1
>>> [i for i, j in enumerate(input_string) if j == '|'][22]
>>> 154

Generates a list containing the index of each occurrence of | 生成一个包含每个|出现索引的列表| and then returns the 23rd element (=22 because lists are zero-indexed) 然后返回第23个元素(= 22,因为列表的索引为零)

Assuming s is input string and you need to find position of nth |: 假设s是输入字符串,则需要找到第n个|的位置:

def find_position(s, n, c):
  return len("".join(s.split(c, maxsplit = n)[:n])) + n - 1

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

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