比较两个整数的相似度

发布于 2024-12-13 04:26:34 字数 298 浏览 3 评论 0原文

示例:

number1 = 54378
number2 = 54379
if number1 (is similar to) number2:
   print (number1 + " " + number2)
   input("what to do")

我想比较这两个数字,并让程序在发生这种(在 number1 和 number2 之间)相似性时通知我。

我希望解决方案能够灵活,并具有更多相似性的空间(_只是第一个数字不同)。

顺便说一句,我正在使用 Python 3.X

Example:

number1 = 54378
number2 = 54379
if number1 (is similar to) number2:
   print (number1 + " " + number2)
   input("what to do")

I would like to compare between these two numbers, and let the program notify me when this kind of (between number1 and number2 ) similarity happens.

I would like the solution to be flexible with some room for more similarities (_ust the first digit is different).

BTW, I am using Python 3.X

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2024-12-20 04:26:34

您可以使用 difflib 为此:

>>> from difflib import SequenceMatcher
>>> number1 = 54378
>>> number2 = 54379
>>> SequenceMatcher(None, str(number1), str(number2)).ratio()
0.80000000000000004

创建 SequenceMatcher 对象及其数字的字符串表示形式,使用 ratio()(或 quick_ratio()real_quick_ratio() 如果速度有问题)获得 0 到 1 之间的相似度评级。

稍微尝试一下后,您可以找出一个好的指标来衡量它们应该有多相似,然后像这样使用它:

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

You can use difflib for this:

>>> from difflib import SequenceMatcher
>>> number1 = 54378
>>> number2 = 54379
>>> SequenceMatcher(None, str(number1), str(number2)).ratio()
0.80000000000000004

After creating a SequenceMatcher object with string representations of their numbers, use ratio() (or quick_ratio() or real_quick_ratio() if speed is an issue) to get a similarity rating between 0 and 1.

After playing around with it a bit you can figure out what a good metric is for how similar they should be, and use it like this:

metric = 0.6   # just an example value
if SequenceMatcher(None, str(a), str(b)).ratio() > metric:
    # a and b are similar
怂人 2024-12-20 04:26:34

您可以执行以下操作之一:
两者都将采用奇数,例如 (100, 10) (200, 12)

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

    return [abs(int(x)-int(y)) if y != None else int(x) for x,y in izip_longest(aS,bS)]

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

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))

返回所有位置差异之和。

you could do one of the following:
Both will take in uneven numbers such as (100, 10) (200, 12)

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

    return [abs(int(x)-int(y)) if y != None else int(x) for x,y in izip_longest(aS,bS)]

returns a list with all the positional differences,

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))

returns the sum difference of all positions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文