gpt4 book ai didi

python - 查找重复的字符(分开)

转载 作者:太空宇宙 更新时间:2023-11-04 01:06:58 26 4
gpt4 key购买 nike

我想找到所有引用这种模式的子字符串:一些字符+一些字符+第一个字符。现在我在 Python 2.7 中有了这个:

T = "i was here"
m = re.findall(r"([a-z])[a-z](\1)", T)
print m

这给出:

[('e', 'e')]

似乎我非常接近实现我的目标,但我不知道如何在这里得到 'ere' 作为结果?

最佳答案

只需在中间字符两边加上括号即可:

>>> m = re.findall(r"([a-z])([a-z])(\1)", T)
>>> m
[('e', 'r', 'e')]
>>> ''.join(m[0])
'ere'

关于python - 查找重复的字符(分开),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29884909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com