Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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 比较两个整数的相似性_Python_Comparison_Similarity - Fatal编程技术网

Python 比较两个整数的相似性

Python 比较两个整数的相似性,python,comparison,similarity,Python,Comparison,Similarity,例如: number1 = 54378 number2 = 54379 if number1 (is similar to) number2: print (number1 + " " + number2) input("what to do") 我想比较这两个数字,并让程序通知我这种(数字1和数字2之间)相似性何时发生 我希望解决方案能够灵活一些,有更多的相似之处(只是第一位数字不同) 顺便说一句,我正在使用Python3.X,您可以执行以下操作之一: 两者都将采用不均匀的数字,

例如:

number1 = 54378
number2 = 54379
if number1 (is similar to) number2:
   print (number1 + " " + number2)
   input("what to do")
我想比较这两个数字,并让程序通知我这种(数字1和数字2之间)相似性何时发生

我希望解决方案能够灵活一些,有更多的相似之处(只是第一位数字不同)


顺便说一句,我正在使用Python3.X,您可以执行以下操作之一:
两者都将采用不均匀的数字,例如
(100,10)
(200,12)

返回包含所有位置差异的列表

from itertools import izip_longest
def findSim(a, b):
    aS = str(a)
    bS = str(b)

    return sum(abs(int(x)-int(y)) if y != None else int(x) for x,y in izip_longest(aS,bS))
返回所有位置的差值总和。

您可以使用:

>>> from difflib import SequenceMatcher
>>> number1 = 54378
>>> number2 = 54379
>>> SequenceMatcher(None, str(number1), str(number2)).ratio()
0.80000000000000004
创建带有数字字符串表示形式的
SequenceMatcher
对象后,使用(或
quick\u ratio()
real\u quick\u ratio()
如果速度是个问题)获得0到1之间的相似性评级

在对其进行一点研究之后,您可以找出一个衡量它们应该有多相似的好指标,并像这样使用它:

metric = 0.6   # just an example value
if SequenceMatcher(None, str(a), str(b)).ratio() > metric:
    # a and b are similar

您尚未定义相似性。请定义相似性。有一个范围吗?彼此相似,不是100%相似,但有些相似。我添加了更多解释。这看起来像是比较两个数字的base10表示,而不是数字本身。。。
metric = 0.6   # just an example value
if SequenceMatcher(None, str(a), str(b)).ratio() > metric:
    # a and b are similar