[英]Python - Compare the similarity of one number and a list of numbers
I am trying to compare the similarity between 1 number and a list of numbers, and not sure how to generate this problem? 我正在尝试比较1个数字和一个数字列表之间的相似性,并且不确定如何产生此问题?
I know how to compare the similarity of 2 inputs: 我知道如何比较两个输入的相似性:
from difflib import SequenceMatcher
def similar(a,b):
return SequenceMatcher(None, a, b).ratio()
a = '123abc'
b = '321321'
similar(a,b)
And now I want to compare the similarity/relevance between 1 number and a list of number, I tried: 现在,我想比较1个数字和一个数字列表之间的相似性/相关性,我尝试过:
A=[1,2,3,4,5,6,7]
B=2
from difflib import SequenceMatcher
def similar(a,b):
return SequenceMatcher(None, a, b).ratio()
similar (A,B)
And it does not give me what I want - it shows "'int' object is not iterable". 而且它没有给我我想要的东西-它显示“'int'对象不可迭代”。 I am trying to get the accuracy/confident of how the number (2) is matching with the list of A. Ideally in this case - if the number is 2, and the list if from 1-7 then similarity is 1, and if the number is 8 or 9 then similarity is 0. 我试图获得数字(2)与A的列表如何匹配的准确性/可信度。在这种情况下,理想的情况是-如果数字为2,并且列表为1-7,则相似度为1,如果数字是8或9,则相似度是0。
Anyone has ideas of how to do it? 任何人都有如何做的想法? I am a new python learner - Thank you in advance! 我是一名新的python学习者-提前谢谢!
Make B a list of length 1 in order to make both objects the same type so they are comparable. 使B的长度为1,以使两个对象具有相同的类型,以便它们具有可比性。
A=[1,2,3,4,5,6,7]
B=[2]
from difflib import SequenceMatcher
def similar(a,b):
return SequenceMatcher(None, a, b).ratio()
similar (A,B)
Have you tried replacing: 您是否尝试过更换:
B=2
with: 有:
B=[2]
? ?
Cheers. 干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.