문제

Alright, I have this code here:

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

I am trying to Input a dictionary like for example this:

L =  {'Cats':{1},'Dogs':{2},'Cat':{0,4},'Dog':{0,4},'Pigs':{3},'Animal':{4}}

And as output I want to have a set that shows dictionary values if the values connect and contain ALL of the input Queries, like this:

query =  [ 'Dog', 'Cat','Animal']     ---->    {4}
query =  [ 'Dog', 'Cat']      --->  {0,4}
query = ['Dog', 'Dogs']   ----  {}      (or set())
query = []  ----->  {}

But the problem is when I try to run the module then I get this strange output:

>>> andSearch(L,Query)
Ellipsis

What could cause that? It is the same no matter what the input is, and it does not give me a error or nothing, can anyone of you smart guys and girls help me out?

도움이 되었습니까?

해결책

I can't reproduce your problem in either Python 2 or 3 using the following slightly updated and optimized code:

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

Output with Python 2.7.5:

query = ['Dog', 'Cat', 'Animal'] ----> set([4])
query = ['Dog', 'Cat'] ----> set([0, 4])
query = ['Dog', 'Dogs'] ----> set([])
query = [] ----> set([])

Output with Python 3.3.2:

query = ['Dog', 'Cat', 'Animal'] ----> {4}
query = ['Dog', 'Cat'] ----> {0, 4}
query = ['Dog', 'Dogs'] ----> set()
query = [] ----> set()

...so I can only assume something is misconfigured or messed-up on your system.
(I don't have 'numpy' or scipy installed, btw.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top