好的,我在这里有这段代码:
def andSearch(inverseIndex, query):
pepp = set()
for y in [inverseIndex[x] for x in query]:
if pepp == set():
pepp.update(y)
else:
pepp & y
return pepp
我正在尝试输入一个字典,例如:
L = {'Cats':{1},'Dogs':{2},'Cat':{0,4},'Dog':{0,4},'Pigs':{3},'Animal':{4}}
作为输出,我希望有一个显示字典值的集合,如果值连接并包含所有输入查询,如下所示:
query = [ 'Dog', 'Cat','Animal'] ----> {4}
query = [ 'Dog', 'Cat'] ---> {0,4}
query = ['Dog', 'Dogs'] ---- {} (or set())
query = [] -----> {}
但问题是当我尝试运行模块时,我得到了这个奇怪的输出:
>>> andSearch(L,Query)
Ellipsis
可能导致什么?无论输入是什么,它都是一样的,它不会给我一个错误或什么都没有,你们这些聪明的家伙和女孩能帮助我吗?
答案 0 :(得分:1)
我无法使用以下略微更新和优化的代码在Python 2或3中重现您的问题:
def andSearch(inverseIndex, query):
pepp = set()
for y in (inverseIndex[x] for x in query):
if not pepp:
pepp.update(y)
else:
pepp &= y
return pepp
L = {'Cats':{1}, 'Dogs':{2}, 'Cat':{0,4}, 'Dog':{0,4}, 'Pigs':{3}, 'Animal':{4}}
for query in [['Dog', 'Cat', 'Animal'],
['Dog', 'Cat'],
['Dog', 'Dogs'],
[],]:
print('query = {} ----> {}'.format(query, andSearch(L, query)))
使用Python 2.7.5输出:
query = ['Dog', 'Cat', 'Animal'] ----> set([4])
query = ['Dog', 'Cat'] ----> set([0, 4])
query = ['Dog', 'Dogs'] ----> set([])
query = [] ----> set([])
使用Python 3.3.2输出:
query = ['Dog', 'Cat', 'Animal'] ----> {4}
query = ['Dog', 'Cat'] ----> {0, 4}
query = ['Dog', 'Dogs'] ----> set()
query = [] ----> set()
...所以我只能假设你的系统出现了错误配置或搞砸的事情
(我没有安装'numpy'或scipy
,顺便说一下。)