Python: 从字符串中的浮点数中删除 0
Python: Remove 0s from floats in string
我正在寻找一种方法,从字符串中的所有浮点数中删除所有无用的零。
所以我会把 1.0*2.40*abc+1
变成 1*2.4*abc+1
。
我现在有两种方法,一种解决了另一种的问题:
re.sub(r'(?<=\d)\.?0+\b', "", my_string)
#Problem: It shorts 10 to 1 (for example)
re.sub(r'(?<=\d)\.0+\b', "", my_string)
#Problem: It doesn't short 2.40 (for example)
如果你不知道我在说什么,尽管问。
好吧,如果您不想使用 re
库,您可以简单地执行此操作。老套路了
string = <your_string_with_zeroes>
string_new = []
for i in list(string):
if i!='0':
string_new.append(i)
print(''.join(string_new))
您正在使用后视模式(`(?<=...) 结构)。这在这里行不通。您需要做的是寻找 "insignificant" 零。 100 中的两个零。100.0100 中的最后两个零不重要。一些注意事项:
100 -> 100
这必须保持不变(当然必须为“0”)。
100.0000 -> 100
这里要删除尾随零和小数点。
100.0100 -> 100.01
这里要保留小数点和小数点到'1'的所有数字。
.1000 -> .1
这里小数点前没有前导数字
一个(不完全)简单的正则表达式就可以解决问题:
re.sub('(\d\.|\.\d*[1-9])0+(?!\d)',r'',string)
这是 (IMO) 最简单的方法,避免了复杂的正则表达式。假设您的所有表达式都由 +、-、* 或 /.
分隔
def strip(s):
if s.find('.') != -1:
s= s.rstrip('0')
if s[-1] == '.': s = s[:-1]
return s
else:
return s
s = raw_input() + '+'
accum = ''
out = ''
for i in range(len(s)):
if s[i] in "+-/*":
out = out + strip(accum) + s[i]
accum = ''
else:
accum = accum + s[i]
print out[:-1]
您可以进行正则表达式替换,使用函数作为替换:
repl=lambda x: x.group(1) + (x.group(2).rstrip('0').rstrip('.'))
re.sub('(?<!\d)(\d+)(\.\d*)(?!\d)',repl,string)
它肯定比更复杂的 RE 更容易。前瞻现在是可选的,但它们不会更改您获得的匹配项。
>>> for testcase in ('1','1.','1.0','1.01','1.01000','0.00','0.100','1.0*2.40*abc+1','100.000','100.0100','20.020','200.'):
... print('{:16s} --> {}'.format(testcase, re.sub('(?<!\d)(\d+)(\.\d*)(?!\d)',repl,testcase)))
...
1 --> 1
1. --> 1
1.0 --> 1
1.01 --> 1.01
1.01000 --> 1.01
0.00 --> 0
0.100 --> 0.1
1.0*2.40*abc+1 --> 1*2.4*abc+1
100.000 --> 100
100.0100 --> 100.01
20.020 --> 20.02
200. --> 200
字符串从哪里来?也许最好从一开始就以您想要的格式生成它,而不是事后尝试解析它。例如......而不是一个完整的解决方案,因为我不知道你可能拥有的价值范围。在 Python 站点上查看 Python 的迷你格式化语言,了解您的 python 版本。格式部分中的数字等显然可以用变量替换
>>> "{:3.0f}*{:3.1f}*{}+{:<3.0F}".format(1.000,2.40000,"abc",1e0)
' 1*2.4*abc+1
我正在寻找一种方法,从字符串中的所有浮点数中删除所有无用的零。
所以我会把 1.0*2.40*abc+1
变成 1*2.4*abc+1
。
我现在有两种方法,一种解决了另一种的问题:
re.sub(r'(?<=\d)\.?0+\b', "", my_string)
#Problem: It shorts 10 to 1 (for example)
re.sub(r'(?<=\d)\.0+\b', "", my_string)
#Problem: It doesn't short 2.40 (for example)
如果你不知道我在说什么,尽管问。
好吧,如果您不想使用 re
库,您可以简单地执行此操作。老套路了
string = <your_string_with_zeroes>
string_new = []
for i in list(string):
if i!='0':
string_new.append(i)
print(''.join(string_new))
您正在使用后视模式(`(?<=...) 结构)。这在这里行不通。您需要做的是寻找 "insignificant" 零。 100 中的两个零。100.0100 中的最后两个零不重要。一些注意事项:
100 -> 100
这必须保持不变(当然必须为“0”)。100.0000 -> 100
这里要删除尾随零和小数点。100.0100 -> 100.01 这里要保留小数点和小数点到'1'的所有数字。
.1000 -> .1
这里小数点前没有前导数字
一个(不完全)简单的正则表达式就可以解决问题:
re.sub('(\d\.|\.\d*[1-9])0+(?!\d)',r'',string)
这是 (IMO) 最简单的方法,避免了复杂的正则表达式。假设您的所有表达式都由 +、-、* 或 /.
分隔def strip(s):
if s.find('.') != -1:
s= s.rstrip('0')
if s[-1] == '.': s = s[:-1]
return s
else:
return s
s = raw_input() + '+'
accum = ''
out = ''
for i in range(len(s)):
if s[i] in "+-/*":
out = out + strip(accum) + s[i]
accum = ''
else:
accum = accum + s[i]
print out[:-1]
您可以进行正则表达式替换,使用函数作为替换:
repl=lambda x: x.group(1) + (x.group(2).rstrip('0').rstrip('.'))
re.sub('(?<!\d)(\d+)(\.\d*)(?!\d)',repl,string)
它肯定比更复杂的 RE 更容易。前瞻现在是可选的,但它们不会更改您获得的匹配项。
>>> for testcase in ('1','1.','1.0','1.01','1.01000','0.00','0.100','1.0*2.40*abc+1','100.000','100.0100','20.020','200.'):
... print('{:16s} --> {}'.format(testcase, re.sub('(?<!\d)(\d+)(\.\d*)(?!\d)',repl,testcase)))
...
1 --> 1
1. --> 1
1.0 --> 1
1.01 --> 1.01
1.01000 --> 1.01
0.00 --> 0
0.100 --> 0.1
1.0*2.40*abc+1 --> 1*2.4*abc+1
100.000 --> 100
100.0100 --> 100.01
20.020 --> 20.02
200. --> 200
字符串从哪里来?也许最好从一开始就以您想要的格式生成它,而不是事后尝试解析它。例如......而不是一个完整的解决方案,因为我不知道你可能拥有的价值范围。在 Python 站点上查看 Python 的迷你格式化语言,了解您的 python 版本。格式部分中的数字等显然可以用变量替换
>>> "{:3.0f}*{:3.1f}*{}+{:<3.0F}".format(1.000,2.40000,"abc",1e0)
' 1*2.4*abc+1