比较两个整数的相似度
示例:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
difflib
为此:创建
SequenceMatcher
对象及其数字的字符串表示形式,使用ratio()
(或quick_ratio()
或real_quick_ratio()
如果速度有问题)获得 0 到 1 之间的相似度评级。稍微尝试一下后,您可以找出一个好的指标来衡量它们应该有多相似,然后像这样使用它:
You can use
difflib
for this:After creating a
SequenceMatcher
object with string representations of their numbers, useratio()
(orquick_ratio()
orreal_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:
您可以执行以下操作之一:
两者都将采用奇数,例如
(100, 10)
(200, 12)
返回包含所有位置差异的列表,
返回所有位置差异之和。
you could do one of the following:
Both will take in uneven numbers such as
(100, 10)
(200, 12)
returns a list with all the positional differences,
returns the sum difference of all positions.