gpt4 book ai didi

python - 在 float python之前插入逗号

转载 作者:行者123 更新时间:2023-12-04 01:06:10 25 4
gpt4 key购买 nike

我是 python 的新手。我有一个 string,如下所示:

a= "1.1 Introduction 1.2 Inverse of a Non-Singular 1.3 Elementary Transformations  1.4 Applications 1.5 Applications of Matrices"
我希望在 String 中的数字之前插入逗号。
我试过的代码:
a= "1.1 Introduction 1.2 Inverse of a Non-Singular 1.3 Elementary Transformations  1.4 Applications 1.5 Applications of Matrices"

b = ""
for i in a:
print(i)
try:
temp = int(i)
b = b+ ","+ temp
except:
b = b + i
但这是错误的。请帮我解决一些问题。
所需输出:
"1.1 Introduction, 1.2 Inverse of a Non-Singular, 1.3 Elementary Transformations,  1.4 Applications, 1.5 Applications of Matrices"

最佳答案

这可能会有所帮助:

import re

a= "10.1 Introduction 10.2 Inverse of a Non-Singular 1.3 Elementary Transformations 1.4 Applications 1.5 Applications of Matrices"

re.sub(r"\s+(\d+\.\d+)", r", \1", a)

关于python - 在 float python之前插入逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66384006/

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