gpt4 book ai didi

python - 删除空格分隔的单个字符

转载 作者:行者123 更新时间:2023-12-03 18:43:04 24 4
gpt4 key购买 nike

我有这样的文本:

the quick brown fox 狐狸 m i c r o s o f t マ イ ク ロ ソ フ ト jumps over the lazy dog 跳過懶狗 best wishes : John Doe
什么是好的正则表达式(对于python)可以删除单个字符,以便输出如下所示:
the quick brown fox 狐狸 jumps over the lazy dog 跳過懶狗 best wishes John Doe
我尝试了 \s{1}\S{1}\s{1}\S{1} 的一些组合,但他们最终不可避免地删除了比我需要的更多的字母。

最佳答案

非正则表达式版本可能如下所示:

source_string = r"this is a string I created"

modified_string =' '.join([x for x in source_string.split() if len(x)>1])

print(modified_string)

关于python - 删除空格分隔的单个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66253193/

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