我正在尝试在python中实现候选消除算法。我需要编写两个函数,一个用于查找更一般的假设,另一个用于查找更具体的假设。 根据定义,“问号”的数量越多,“零”的数量越少意味着越一般,反之亦然。 但我的功能不起作用。他们只会返回虚假。问题出在哪里?
def more_specific(a, b):
"""
Checks if a is more specific than b.
"""
aa = 1
bb = 1
for i in range(len(a)):
if a[i] == '0':
aa+=1
elif a[i] == '?':
aa-=1
for k in range(len(b)):
if b[k] == '0':
bb+=1
elif b[k] == '?':
bb-=1
return aa > bb
def more_general(a, b):
"""
Checks if a is more general than b.
"""
aa = 1
bb = 1
for i in range(len(a)):
if a[i] == '?':
aa+=1
elif a[i] == '0':
aa-=1
for k in range(len(b)):
if b[k] == '?':
bb+=1
elif b[k] == '0':
bb-=1
return aa > bb
试试这个例子:
a = [('far','?', '?', '?')]
b = [('ss' , '0','0','i')]
答案 0 :(得分:0)
Your data for a
and b
has the wrong format. Just remove the outside []
:
>>> a = ('far','?', '?', '?')
>>> b = ('ss' , '0','0','i')
>>> more_specific(b, a)
True
Your list has only one element that is a tuple:
>>> len([('far','?', '?', '?')])
1
But you want to go through all of the elements of the tuple:
>>> len(('far','?', '?', '?'))
4