gpt4 book ai didi

python - 使用多个参数拆分字符串

转载 作者:太空宇宙 更新时间:2023-11-03 13:08:17 28 4
gpt4 key购买 nike

我有一个字符串

Best product25.075.0Product29.029.0

现在我需要将这个字符串拆分为

'Best product' '25.0' '75.0' , 'Product' '29.0' '29.0'

我怎样才能做到这一点?

最佳答案

您可以使用 re.findall 来查找所有单词(包含字母或空格 - 匹配模式 [a-zA-Z ]+)或所有数字(一个或更多数字后跟一个点和零 - 匹配模式 \d+.0)

string = 'Best product25.075.0Product29.029.0'
import re
re.findall(r'[a-zA-Z ]+|\d+(?:.0)?', string)
# ['Best product', '25.0', '75.0', 'Product', '29.0', '29.0']

关于python - 使用多个参数拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51079127/

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