python - 替换之前出现的字符串
问题描述
我想删除括号内的重复单词并将它们替换为“S”+单词。
例如:
(Skipper Skipper) -> (S Skipper)
('s 's) -> (S 's)
这是字符串,s
:
s = "(S (S (S (S (Skipper Skipper) ('s 's)) (Inc. Inc.)) (S (Bellevue Bellevue) (Wash. Wash.)))
(S (said said) (S (it it) (S (signed signed) (S (a a) (S (definitive definitive)
(S (merger merger) (S (agreement agreement) (S (for for) (S (S (a a)
(S (National National) (S (Pizza Pizza) (S (Corp. Corp.) (unit unit)))))
(S (to to) (S (acquire acquire) (S (S (S (the the) (S (90.6 90.6) (% %)))
(S (S (of of) (S (S (Skipper Skipper) ('s 's)) (Inc. Inc.))) (S (it it)
(S (does does) (S (n't n't) (own own)))))) (S (for for) (S (S (11.50 11.50)
(S (a a) (share share))) (S (or or) (S (about about) (S (28.1 28.1) (million million)))))))))))))))))))"
预期结果:
out = "(S (S (S (S (S Skipper) (S 's)) (S Inc.)) (S (S Bellevue) (S Wash.)))
(S (S said) (S (S it) (S (S signed) (S (S a) (S (S definitive) (S (S merger)
(S (S agreement) (S (S for) (S (S (S a) (S (S National) (S (S Pizza) (S (S Corp.)
(S unit))))) (S (S to) (S (S acquire) (S (S (S (S the) (S (S 90.6) (S %))) (S (S (S of)
(S (S (S Skipper) (S 's)) (S Inc.))) (S (S it) (S (S does) (S (S n't) (S own))))))
(S (S for) (S (S (S 11.50) (S (S a) (S share))) (S (S or) (S (S about) (S (S 28.1) (S million)))))))))))))))))))"
我试着做:
from collections import Counter
lst = s.lstrip("(").rstrip(")").replace("(", "").replace(")", "").split()
d = Counter(lst)
mapper = {((k + " ") * v).strip():"S" + " " + k for k, v in d.items()}
for k, v in mapper.items():
out = s.replace(k, v)
但不是很正确:
out = "(S (S (S (S (Skipper Skipper) ('s 's)) (Inc. Inc.)) (S (S Bellevue) (S Wash.)))
(S (S said) (S (it it) (S (S signed) (S (a a) (S (S definitive) (S (S merger)
(S (S agreement) (S (for for) (S (S (a a) (S (S National) (S (S Pizza) (S (S Corp.)
(S unit))))) (S (S to) (S (S acquire) (S (S (S (S the) (S (S 90.6) (S %))) (S (S (S of)
(S (S (Skipper Skipper) ('s 's)) (Inc. Inc.))) (S (it it) (S (S does) (S (S n't) (S own))))))
(S (for for) (S (S (S 11.50) (S (a a) (S share))) (S (S or) (S (S about) (S (S 28.1) (S million)))))))))))))))))))"
解决方案
用于re.sub
替换它们:
import re
def sub(matched):
return f"(S {matched.group(2)})" if matched.group(1) == matched.group(2) else str(matched.groups())
s = '''(S (S (S (S (Skipper Skipper) ('s 's)) (Inc. Inc.)) (S (Bellevue Bellevue) (Wash. Wash.)))
(S (said said) (S (it it) (S (signed signed) (S (a a) (S (definitive definitive)
(S (merger merger) (S (agreement agreement) (S (for for) (S (S (a a)
(S (National National) (S (Pizza Pizza) (S (Corp. Corp.) (unit unit)))))
(S (to to) (S (acquire acquire) (S (S (S (the the) (S (90.6 90.6) (% %)))
(S (S (of of) (S (S (Skipper Skipper) ('s 's)) (Inc. Inc.))) (S (it it)
(S (does does) (S (n't n't) (own own)))))) (S (for for) (S (S (11.50 11.50)
(S (a a) (share share))) (S (or or) (S (about about) (S (28.1 28.1) (million million)))))))))))))))))))'''
result = re.sub(r"\(([\.\%\'\w\d]+) ([\.\%\'\w\d]+)\)", sub, s)
推荐阅读
- vue.js - 将动态插槽从父级传递到子级到孙子级
- snowflake-cloud-data-platform - 将大量小文件加载到雪花中
- amazon-web-services - 使用 SAM AWS 部署带有 HTTP 端点集成的 REST API?
- python - 如何使用默认填充值将 2d numpy 数组的所有内部值设置为相同的大小
- javascript - Cloud Firestore onSnapshot() 仅在没有初始状态的更改时触发
- javascript - 在js中将div旋转到一定高度
- c++ - 我可以以相同的方式使用 char* c 和字符串吗?
- java - android中的Webview不会从res/drawable加载图像
- java - Polarion ALM JAVA SDK API WorkItem 获取类型表示
- authentication - 关于身份验证令牌的 insomnia bitbucket git 配置