Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将一个数字与另一个数字的最后n位匹配_Python_Python 2.7 - Fatal编程技术网

Python 将一个数字与另一个数字的最后n位匹配

Python 将一个数字与另一个数字的最后n位匹配,python,python-2.7,Python,Python 2.7,我有一对数字的列表,例如: 1.45 4 0.73 17 0.201 18 509 24 0.55 21 这里第二个数字是第一个值的错误。在文献中,为了方便起见,你会看到它写为1.45(4),而实际上它应该是1.45+/-0.04。我希望将第二个数字转换为“真实”错误,即我希望将上述列表转换为: 1.45 0.04 0.73 0.17 0.201 0.018 509 24 0.55 0.21 迄今为止,我最大的成功在于以下功能: def gMatchError(fValue, fError)

我有一对数字的列表,例如:

1.45 4
0.73 17
0.201 18
509 24
0.55 21
这里第二个数字是第一个值的错误。在文献中,为了方便起见,你会看到它写为1.45(4),而实际上它应该是1.45+/-0.04。我希望将第二个数字转换为“真实”错误,即我希望将上述列表转换为:

1.45 0.04
0.73 0.17
0.201 0.018
509 24
0.55 0.21
迄今为止,我最大的成功在于以下功能:

def gMatchError(fValue, fError):
  sExponent = -10
  while True:
    fFindDecimal = fValue*math.pow(10, sExponent)
    if fFindDecimal.is_integer():
      return fError / math.pow(10, sExponent)
    sExponent += 1  
它迭代地将第一个值乘以10^i,直到它是一个整数。当它是整数时,误差可以除以10^i

这适用于上面列出的所有数字,最后一个除外,返回0.021。我知道这是因为浮点存储在内存中的方式的性质,即使用。repr()我可以看到它检查55.000000001是否为整数,当然不是

我也试着和你核实一下

if int(fFindDecimal) == int(fFindDecimal+0.9) and int(fFindDecimal) != 0:
  return (as above)
<>但是当中间有0,例如在0.201种情况下,失败。


在所有情况下,是否都有可行的替代方案?提前感谢。

正如您所了解的,十进制值通常不完全以二进制浮点形式存储。相反,我建议您在阅读时使用数字的文本版本。也许是这样的:

measure, error = input_line.split()
whole, frac = measure.split('.')
precision = len(frac)
error = float(error) / 10**precision

print measure, error
我给你留下了几个不雅的地方:

  • 我悄悄地将错误从字符串转换为浮点
  • 。。。而度量值仍然是一个字符串
  • 我忽略了整数度量的情况;整条,frac=语句将出错
但是,我希望这将使您顺利地找到解决方案。

我将使用以下模块:

这也可以处理像我将使用的0.500这样的数字

def get_precision(num, precision):
    if num % 1:
        precision = precision * 10 ** -len(str(num).split('.')[1])
    return precision
给予

>>> get_precision(123.456,16)
0.016
>>> get_precision(123,16)
16
>>> get_precision(12231233.1,16)
1.6
一些解释:

  • 如果数字不是整数,因此包含小数点,则
    num%1
    条件返回true
  • len(str(num).split('.')[1])
    提供小数点后的位数
输出:

['1.45 0.04', '0.73 0.17', '0.201 0.018', '509 24', '0.55 0.21']

我将根据第一个数字的字符串表示确定精度(例如,您无法从浮点变量中区分10.0和10.00)。使用正则表达式,您可以获得具有组的不同元素

VALUES = (
  '1.45 4',
  '0.73 17',
  '0.201 18',
  '509 24',
  '0.55 21',
)

import re
REGEX = re.compile( r"(\d+(\.(\d+))?)\s(\d+)" )

for v in VALUES:
  m = REGEX.match(v)
  if m.group(3) is not None:
    nd = len(m.group(3))   # nb of decimals in first number          
    d = 0.1 ** nd
    n2 = float(m.group(4)) * d
    print "{n1} {n2:{nd}}".format(n1=m.group(1), n2=n2, nd=nd)
  else:
    print v
哪张照片

1.45 0.04
0.73 0.17
0.201 0.018
509 24
0.55 0.21

1.50+/-0.01如何符合您的列表?看起来里面有浮点数和整数,所以会被截断为
1.51
,不是吗?这些数字是如何进入名单的?例如,您是从文本文件中读取它们的吗?非常好的一点是,它将被截断,这样函数将从1.50(1)返回0.1,而不是所需的0.01。我没有考虑过这一点,因为我还没有看到它出现在列表中,但它可能会发生。
VALUES = (
  '1.45 4',
  '0.73 17',
  '0.201 18',
  '509 24',
  '0.55 21',
)

import re
REGEX = re.compile( r"(\d+(\.(\d+))?)\s(\d+)" )

for v in VALUES:
  m = REGEX.match(v)
  if m.group(3) is not None:
    nd = len(m.group(3))   # nb of decimals in first number          
    d = 0.1 ** nd
    n2 = float(m.group(4)) * d
    print "{n1} {n2:{nd}}".format(n1=m.group(1), n2=n2, nd=nd)
  else:
    print v
1.45 0.04
0.73 0.17
0.201 0.018
509 24
0.55 0.21