检查字符串是否以 Python 中的小数结尾 2
Check if a string ends with a decimal in Python 2
我想检查一个字符串是否以不同数字的小数结尾,从搜索了一段时间,我发现最接近的解决方案是将值输入一个元组并将其用作endswith() 的条件。但是有没有更短的方法而不是输入所有可能的组合?
我尝试对结束条件进行硬编码,但如果列表中有新元素,它对这些元素不起作用,我也尝试使用正则表达式 returns 其他元素以及小数元素。任何帮助将不胜感激
list1 = ["abcd 1.01", "zyx 22.98", "efgh 3.0", "qwe -70"]
for e in list1:
if e.endswith('.0') or e.endswith('.98'):
print 'pass'
编辑:抱歉应该指定我不想让 'qwe -70' 被接受,只应接受那些带小数点的元素
您的示例数据留下了许多可能性:
最后一个字符是数字:
e[-1].isdigit()
最后一个 space 之后的所有内容都是数字:
try:
float(e.rsplit(None, 1)[-1])
except ValueError:
# no number
pass
else:
print "number"
使用正则表达式:
re.match('[.0-9]$', e)
suspects = [x.split() for x in list1] # split by the space in between and get the second item as in your strings
# iterate over to try and cast it to float -- if not it will raise ValueError exception
for x in suspects:
try:
float(x[1])
print "{} - ends with float".format(str(" ".join(x)))
except ValueError:
print "{} - does not ends with float".format(str(" ".join(x)))
## -- End pasted text --
abcd 1.01 - ends with float
zyx 22.98 - ends with float
efgh 3.0 - ends with float
qwe -70 - ends with float
我认为这适用于这种情况:
regex = r"([0-9]+\.[0-9]+)"
list1 = ["abcd 1.01", "zyx 22.98", "efgh 3.0", "qwe -70"]
for e in list1:
str = e.split(' ')[1]
if re.search(regex, str):
print True #Code for yes condition
else:
print False #Code for no condition
我想提出另一种解决方案:使用 regular expressions 搜索结尾小数。
您可以使用以下正则表达式 [-+]?[0-9]*\.[0-9]+$
.
为小数点后缀定义正则表达式
正则表达式分解:
[-+]?
: 开头可选-或+符号
[0-9]*
:零个或多个数字
\.
: 需要点
[0-9]+
:一个或多个数字
$
: 必须在行尾
然后我们可以测试正则表达式,看看它是否匹配列表中的任何成员:
import re
regex = re.compile('[-+]?[0-9]*\.[0-9]+$')
list1 = ["abcd 1.01", "zyx 22.98", "efgh 3.0", "qwe -70", "test"]
for e in list1:
if regex.search(e) is not None:
print e + " passes"
else:
print e + " does not pass"
上一个脚本的输出如下:
abcd 1.01 passes
zyx 22.98 passes
efgh 3.0 passes
qwe -70 does not pass
test does not pass
正如您猜对的那样,考虑到组合的数量基本上是无限的,endswith()
不是查看解决方案的好方法。要走的路是——正如许多人建议的那样——一个正则表达式,它将字符串的末尾匹配为小数点后跟任意位数。除此之外,保持代码简单易读。 strip()
在那里,以防万一输入字符串末尾有一个额外的 space,这会使正则表达式不必要地复杂化。
您可以在以下位置查看实际效果:https://eval.in/649155
import re
regex = r"[0-9]+\.[0-9]+$"
list1 = ["abcd 1.01", "zyx 22.98", "efgh 3.0", "qwe -70"]
for e in list1:
if re.search(regex, e.strip()):
print e, 'pass'
流动可能有帮助:
import re
reg = re.compile(r'^[a-z]+ \-?[0-9]+\.[0-9]+$')
if re.match(reg, the_string):
do something...
else:
do other...
我想检查一个字符串是否以不同数字的小数结尾,从搜索了一段时间,我发现最接近的解决方案是将值输入一个元组并将其用作endswith() 的条件。但是有没有更短的方法而不是输入所有可能的组合?
我尝试对结束条件进行硬编码,但如果列表中有新元素,它对这些元素不起作用,我也尝试使用正则表达式 returns 其他元素以及小数元素。任何帮助将不胜感激
list1 = ["abcd 1.01", "zyx 22.98", "efgh 3.0", "qwe -70"]
for e in list1:
if e.endswith('.0') or e.endswith('.98'):
print 'pass'
编辑:抱歉应该指定我不想让 'qwe -70' 被接受,只应接受那些带小数点的元素
您的示例数据留下了许多可能性:
最后一个字符是数字:
e[-1].isdigit()
最后一个 space 之后的所有内容都是数字:
try:
float(e.rsplit(None, 1)[-1])
except ValueError:
# no number
pass
else:
print "number"
使用正则表达式:
re.match('[.0-9]$', e)
suspects = [x.split() for x in list1] # split by the space in between and get the second item as in your strings
# iterate over to try and cast it to float -- if not it will raise ValueError exception
for x in suspects:
try:
float(x[1])
print "{} - ends with float".format(str(" ".join(x)))
except ValueError:
print "{} - does not ends with float".format(str(" ".join(x)))
## -- End pasted text --
abcd 1.01 - ends with float
zyx 22.98 - ends with float
efgh 3.0 - ends with float
qwe -70 - ends with float
我认为这适用于这种情况:
regex = r"([0-9]+\.[0-9]+)"
list1 = ["abcd 1.01", "zyx 22.98", "efgh 3.0", "qwe -70"]
for e in list1:
str = e.split(' ')[1]
if re.search(regex, str):
print True #Code for yes condition
else:
print False #Code for no condition
我想提出另一种解决方案:使用 regular expressions 搜索结尾小数。
您可以使用以下正则表达式 [-+]?[0-9]*\.[0-9]+$
.
正则表达式分解:
[-+]?
: 开头可选-或+符号[0-9]*
:零个或多个数字\.
: 需要点[0-9]+
:一个或多个数字$
: 必须在行尾
然后我们可以测试正则表达式,看看它是否匹配列表中的任何成员:
import re
regex = re.compile('[-+]?[0-9]*\.[0-9]+$')
list1 = ["abcd 1.01", "zyx 22.98", "efgh 3.0", "qwe -70", "test"]
for e in list1:
if regex.search(e) is not None:
print e + " passes"
else:
print e + " does not pass"
上一个脚本的输出如下:
abcd 1.01 passes
zyx 22.98 passes
efgh 3.0 passes
qwe -70 does not pass
test does not pass
正如您猜对的那样,考虑到组合的数量基本上是无限的,endswith()
不是查看解决方案的好方法。要走的路是——正如许多人建议的那样——一个正则表达式,它将字符串的末尾匹配为小数点后跟任意位数。除此之外,保持代码简单易读。 strip()
在那里,以防万一输入字符串末尾有一个额外的 space,这会使正则表达式不必要地复杂化。
您可以在以下位置查看实际效果:https://eval.in/649155
import re
regex = r"[0-9]+\.[0-9]+$"
list1 = ["abcd 1.01", "zyx 22.98", "efgh 3.0", "qwe -70"]
for e in list1:
if re.search(regex, e.strip()):
print e, 'pass'
流动可能有帮助:
import re
reg = re.compile(r'^[a-z]+ \-?[0-9]+\.[0-9]+$')
if re.match(reg, the_string):
do something...
else:
do other...