gpt4 book ai didi

python - 如何在列表中组合相似的字符?

转载 作者:太空狗 更新时间:2023-10-29 18:20:51 25 4
gpt4 key购买 nike

我正在尝试组合列表中彼此相邻的相似字符。我想知道是否有 Python 方法可以做到这一点?这是一个例子:

test = 'hello###_world###test#test123##'
splitter = re.split("(#)", test)
splitter = filter(None, splitter)

在 splitter 变量中返回:

['hello', '#', '#', '#', '_world', '#', '#', '#', 'test', '#', 'test123', '#', '#']

我正在尝试组合哈希,因此列表变成了这样:

['hello', '###', '_world', '###', 'test', '#', 'test123', '##']

感谢您的帮助!

最佳答案

尝试:

splitter = re.split("(#+)", test)

关于python - 如何在列表中组合相似的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54603094/

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