简体   繁体   English

python字符串按出现次数替换字母

[英]python string substitute letter by its occurrence number

Suppose I have a string Hello, world , and I'd like to replace all instances of o by its occurrence number, ie get a sting: Hell1, w2rld ?假设我有一个字符串Hello, world ,我想用它的出现次数替换o的所有实例,即得到一个刺痛: Hell1, w2rld

I have found how to reference a numbered group, \\g<1> , but it does require a group number.我找到了如何引用编号组\\g<1> ,但它确实需要一个组号。

Is there are way to do what I want in python?有没有办法在python中做我想做的事?

Update: Sorry for not mentioning that I was indeed looking for a regexp solution, not just a string.更新:很抱歉没有提到我确实在寻找正则表达式解决方案,而不仅仅是字符串。 I have marked the solution I liked the best, but thank for all contributions, they were cool!我已经标记了我最喜欢的解决方案,但感谢所有贡献,他们很酷!

For a regular expression solution :对于正则表达式解决方案

import re

class Replacer:
    def __init__(self):
        self.counter = 0

    def __call__(self, mo):
        self.counter += 1
        return str(self.counter)

s = 'Hello, World!'
print(re.sub('o', Replacer(), s))

Split the string on the letter "o" and reassemble it by adding the index of the part in front of each part (except the first one):拆分字母“o”上的字符串并通过在每个部分(第一个除外)前面添加部分的索引来重新组合它:

string = "Hello World"

result = "".join(f"{i or ''}"+part for i,part in enumerate(string.split("o")))

output:输出:

print(result)

# Hell1 W2rld

Using itertools.count使用itertools.count

Ex:前任:

import re
from itertools import count 

c = count(1)
s = "Hello, world"

print(re.sub(r"o", lambda x: "{}".format(next(c)), s))
#or
print(re.sub(r"o", lambda x: f"{next(c)}", s))
# --> Hell1, w2rld

You don't need regex for that, a simple loop will suffice:您不需要正则表达式,一个简单的循环就足够了:

sample = "Hello, world"

pattern = 'o'
output = ''
count = 1
for char in sample:
    if char == pattern:
        output += str(count)
        count += 1
    else:
        output += char

print(output)
>>> Hell1, w2rld

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

相关问题 Python正则表达式:替换所有出现的特定字符串,而不是在特定字符之后 - Python regex: substitute all occurrence of a certain string NOT after a specific character 如何识别单词中的字符并将该字母替换为另一个字符串-Python - How to identify a character within a word and substitute that letter into another string - Python 使用python查找txt文件中字母出现的次数 - find the number of occurrence of letter in a txt file using python 如何在Python中首次出现字母时拆分字符串? - How can I split a string at the first occurrence of a letter in Python? Python删除字符串的最后一个字符,如果它是一个字母 - Python remove last character of string if its a letter 如何使用正则表达式仅匹配位于浮点数之后的字母并替换另一个字符串 - How to use regex to match only a letter positioned after a float number and substitute for another string 在 python 中计算字符串中的数字、字母和特殊字符 - Counting number, letter and special characters in a string in python Python - Pandas - 计算字符串中出现的字符数并替换字符串值 - Python - Pandas - Count the number of character occurrence in a string and replace the string value Python字符串替代 - Python string substitute 计算字符串中出现的次数 - counting number of occurrence in string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM