gpt4 book ai didi

python - 如何最好地将多个空格替换为一个(在 python 中)?

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

<分区>

例如,在 python 中,我有以下字符串:

a = "Sentence  with     weird    whitespaces"

我想用一个扩展空格替换同一个字符串,所以最后的字符串会是这样

 'Sentence with weird whitespaces'

我自己找到了解决方案,但是有更好/更短的解决方案吗?

' '.join([x for x in a.split(' ') if len(x)>0])

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