Python-替换后如何恢复原始字符串?

时间:2019-05-05 13:35:54

标签: python

比方说,原始字符串是:

LEFT JOIN

现在,通过执行以下操作,将上述字符串中的所有“ D6”实例替换为“ 9F”:

a = 'DA115792C339A5E674416AB0559BF5CB8D38E88B1E71F4DFAE896EBD2D13ABB13FFB1DC687DCF0C270A8BF4861D2E6D26E74DC81C18EB53FD5C52AA691A8F16BDA4E1EB8009B00DCD61457E34C438F23EF3D1FD905CF689793CC1E02E1ECB6778A1E2720D416AC432959A8A3B20B43525A856C97E4D404065D1D30ADC74D012B27D00B0029AD3940CB2C9F2AB6C01D430F3A58584C5DB6C98839579AF2A8F90A5D80B5097D547105DF2D9F9485E9F2CCFD6F9ECDDBD562FE09EA81C19B18482BD483AEBAB8481EE208887909DDAE826629538F36E6A50CEECBF3462E9FFBDAC6363F3A9A56F31081EBF28AD0FCF288B0DB8CB44735B9D7E6D193D55C90767E83'

现在,让我们说,我们只有b的值,而没有a。我们需要对b执行一些操作以检索a的原始值。

此外,我们知道'a'的值应满足一定的数学条件。

数学条件为:

数字z应该可被(z%a == 0)整除

b = a.replace('D6','9F')

b = 'DA115792C339A5E674416AB0559BF5CB8D38E88B1E71F4DFAE896EBD2D13ABB13FFB1DC687DCF0C270A8BF4861D2E6D26E74DC81C18EB53FD5C52AA691A8F16BDA4E1EB8009B00DC9F1457E34C438F23EF3D1FD905CF689793CC1E02E1ECB6778A1E2720D416AC432959A8A3B20B43525A856C97E4D404065D1D30ADC74D012B27D00B0029AD3940CB2C9F2AB6C01D430F3A58584C5DB6C98839579AF2A8F90A5D80B5097D547105DF2D9F9485E9F2CCF9FF9ECDDBD562FE09EA81C19B18482BD483AEBAB8481EE208887909DDAE826629538F36E6A50CEECBF3462E9FFBDAC6363F3A9A56F31081EBF28AD0FCF288B0DB8CB44735B9D7E6D193D55C90767E83'

我们需要利用'a'的数学属性从b恢复它。

我知道我们不能在b上使用replace()函数取回'a',因为某些'9F'的原始实例可能会被替换。

我认为我们需要在不同的位置执行替换操作,直到满足该条件为止。

对于给定的字符串,我们有:

z = 55057004365075793824891923502198296150348187500859129529014955509148421282041969078213265169463529503768779794209446773790749529176461595867792548236095966024387560672845152234957439383409540755826755640123124159246487058454615922008741879614211920551517049373314503998980825185719370304183623398662036133862488876163410866971729000216470924616148028986990798495248878127793311548452974671645100371499570058070179424193067736979204502413302335974105838586819414807952974885796840178274113497125765593996690493177955553456655538977929256055738007112424150644005452979891672942537126552535517394691741201589304958975238

因此,我们必须在不同的位置组合中将'9F'替换为'D6',直到获得满足数学条件'C'的'a'。

我做了类似的事情:

b.count('9F')
6

这似乎是与排列和组合有关的问题。

2 个答案:

答案 0 :(得分:3)

您的原始字符串中已经有49F实例。这些正在引起怪异的行为

a = 'DA115792C339A5E674416AB0559BF5CB8D38E88B1E71F4DFAE896EBD2D13ABB13FFB1DC687DCF0C270A8BF4861D2E6D26E74DC81C18EB53FD5C52AA691A8F16BDA4E1EB8009B00DCD61457E34C438F23EF3D1FD905CF689793CC1E02E1ECB6778A1E2720D416AC432959A8A3B20B43525A856C97E4D404065D1D30ADC74D012B27D00B0029AD3940CB2C9F2AB6C01D430F3A58584C5DB6C98839579AF2A8F90A5D80B5097D547105DF2D9F9485E9F2CCFD6F9ECDDBD562FE09EA81C19B18482BD483AEBAB8481EE208887909DDAE826629538F36E6A50CEECBF3462E9FFBDAC6363F3A9A56F31081EBF28AD0FCF288B0DB8CB44735B9D7E6D193D55C90767E83'

print(a.count('9F'))
#4

否则string.replace会完美运行

a = 'hello'
b = a.replace('l','a')
print(b)
#heaao
c = b.replace('a','l')
print(c)
#hello
print( a == c)
#True

给您True(将l转换为a,然后将a转换为l

可能的解决方案,将现有的9F替换为字符串中不存在的内容,例如XY,然后将其也用于替换

a = 'DA115792C339A5E674416AB0559BF5CB8D38E88B1E71F4DFAE896EBD2D13ABB13FFB1DC687DCF0C270A8BF4861D2E6D26E74DC81C18EB53FD5C52AA691A8F16BDA4E1EB8009B00DCD61457E34C438F23EF3D1FD905CF689793CC1E02E1ECB6778A1E2720D416AC432959A8A3B20B43525A856C97E4D404065D1D30ADC74D012B27D00B0029AD3940CB2C9F2AB6C01D430F3A58584C5DB6C98839579AF2A8F90A5D80B5097D547105DF2D9F9485E9F2CCFD6F9ECDDBD562FE09EA81C19B18482BD483AEBAB8481EE208887909DDAE826629538F36E6A50CEECBF3462E9FFBDAC6363F3A9A56F31081EBF28AD0FCF288B0DB8CB44735B9D7E6D193D55C90767E83'

#Replace 9F to XY, then D6 to 9F
b = a.replace('9F','XY').replace('D6','9F')
#Replace 9F to D6, then XY to 9F
c = b.replace('9F', 'D6').replace('XY', '9F')

print(a == c)
#True

答案 1 :(得分:1)

在这两个操作之后,您将不会找回原始字符串,因为您的原始字符串中也可能也包含“ 9F”:

test = "D69F"

changed = test.replace("D6", "9F")
print(changed)
# 9F9F

undo_change = changed.replace("9F", "D6")
print(undo_change)
# D6D6

如果它不干扰您的中间步骤,则可以屏蔽替换项,例如与test.replace("D6", "§9F")