Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换python中的模式_Python - Fatal编程技术网

替换python中的模式

替换python中的模式,python,Python,如何将字符串中的模式替换为 decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)" The first pattern "(++info++)" needs to replaced with (++info a++) The second pattern "(++info++)" needs to replaced with (++info b++) The third pattern "(++

如何将字符串中的模式替换为

     decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)"

 The first pattern "(++info++)" needs to replaced with (++info a++)
 The second pattern "(++info++)" needs to replaced with (++info b++)
 The third pattern "(++info++)" needs to replaced with (++info c++)
 If there many more then it should be replaced accordingly

下面是一个快速解决方法:

string=" Name(++info++)Age(++info++)Adress of the emp(++info++)"

def doit(s):
    import string
    allTheLetters = list(string.lowercase)
    i=0
    s2 = s.replace("++info++","++info "+allTheLetters[i]+"++",1)
    while (s2!=s):
        s=s2
        i=i+1
        s2 = s.replace("++info++","++info "+allTheLetters[i]+"++",1)
    return s

请注意,性能可能不是很好。

这里有一个快速解决方法:

string=" Name(++info++)Age(++info++)Adress of the emp(++info++)"

def doit(s):
    import string
    allTheLetters = list(string.lowercase)
    i=0
    s2 = s.replace("++info++","++info "+allTheLetters[i]+"++",1)
    while (s2!=s):
        s=s2
        i=i+1
        s2 = s.replace("++info++","++info "+allTheLetters[i]+"++",1)
    return s

请注意,性能可能不是很好。

这里有一个相当丑陋但实用的解决方案:

import string

decoded_str = " Name(++info++)Age(++info++)Adress of the emp(++info++)"
letters = list(string.lowercase)
token = "(++info++)"
rep_token = "(++info %s++)"

i = 0
while (token in decoded_str):
    decoded_str = decoded_str.replace(token, rep_token % letters[i], 1)
    i += 1

print decoded_str

这是一个相当丑陋但实际的解决方案:

import string

decoded_str = " Name(++info++)Age(++info++)Adress of the emp(++info++)"
letters = list(string.lowercase)
token = "(++info++)"
rep_token = "(++info %s++)"

i = 0
while (token in decoded_str):
    decoded_str = decoded_str.replace(token, rep_token % letters[i], 1)
    i += 1

print decoded_str

这应该足够简单:

for character in range(ord('a'), ord('z')):
    if "(++info++)" not in decoded_str:
        break
    decoded_str = decoded_str.replace("(++info++)", "(++info {0}++)".format(chr(character)), 1)

print decoded_str
它还有在“z”处停止的额外好处。如果您想环绕:

import itertools

for character in itertools.cycle(range(ord('a'), ord('z'))):
    if "(++info++)" not in decoded_str:
        break
    decoded_str = decoded_str.replace("(++info++)", "(++info {0}++)".format(chr(character)), 1)

print decoded_str
只是为了好玩,一行,和O(n):


这应该足够简单:

for character in range(ord('a'), ord('z')):
    if "(++info++)" not in decoded_str:
        break
    decoded_str = decoded_str.replace("(++info++)", "(++info {0}++)".format(chr(character)), 1)

print decoded_str
它还有在“z”处停止的额外好处。如果您想环绕:

import itertools

for character in itertools.cycle(range(ord('a'), ord('z'))):
    if "(++info++)" not in decoded_str:
        break
    decoded_str = decoded_str.replace("(++info++)", "(++info {0}++)".format(chr(character)), 1)

print decoded_str
只是为了好玩,一行,和O(n):


这里有一些很好的O(N)答案,为什么要接受其中一个O(N^2)答案?@gnibler您介意标记O(N)答案吗?我不太习惯看那种事情的真实例子。这里的N是指“++info++”元素的计数吗?@aaron,O(N^2)答案是为每个(++info++)复制整个字符串的答案。在这种情况下,主要是那些为
replace()
使用
1
参数的人,我可能是错的,但我感觉这些字符串将相当短。@gnibbler。谢谢我现在明白了。我认为我的错误是不知道字符串操作设备以及我应该。再次感谢。这里有一些很好的O(N)答案,为什么要接受其中一个O(N^2)答案?@gnibler您介意标记O(N)个吗?我不太习惯看那种事情的真实例子。这里的N是指“++info++”元素的计数吗?@aaron,O(N^2)答案是为每个(++info++)复制整个字符串的答案。在这种情况下,主要是那些为
replace()
使用
1
参数的人,我可能是错的,但我感觉这些字符串将相当短。@gnibbler。谢谢我现在明白了。我认为我的错误是不知道字符串操作设备以及我应该。再次感谢。谢谢你的例子。它给了我一个新的想法,让我想起了我已经忘记的五件事。谢谢你的例子。它给了我一个新的想法,让我想起了我已经忘记的五件事。
import string

decoded_str = " Name(++info++)Age(++info++)Adress of the emp(++info++)"
s = decoded_str.replace('++info++', '++info %s++')
s % tuple(i for i in string.ascii_lowercase[:s.count('%s')])
from itertools import izip
import string
decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)"
parts = iter(decoded_str.split("(++info++)"))
first_part = next(parts)
tags = iter(string.ascii_lowercase)
encoded_str=first_part+"".join("(++info %s++)%s"%x for x in izip(tags, parts))
print encoded_str