Python多字索引

时间:2012-09-24 20:13:24

标签: python list dictionary

index = {'Michael': [['mj.com',1], ['Nine.com',9],['i.com', 34]], / 
         'Jackson': [['One.com',4],['mj.com', 2],['Nine.com', 10], ['i.com', 45]], /
         'Thriller' : [['Seven.com', 7], ['Ten.com',10], ['One.com', 5], ['mj.com',3]}

# In this dictionary (index), for eg: 'KEYWORD': 
# [['THE LINK in which KEYWORD is present,'POSITION
# of KEYWORD in the page specified by link']]
  

例如:Michael出现在MJ.com,NINE.com和i.com的相应页面的第1,9,34位。

请帮我一个以indexKEYWORDS为输入的python程序。

当我输入'MICHAEL'时。结果应该是:

>>['mj.com', 'nine.com', 'i.com']

当我输入'MICHAEL JACKSON'.时,结果应为:

>>['mj.com', 'Nine.com']

Michael'和”Jackson'连续出现在'mj.com''nine.com',即位置(1,2)和& (9,10)分别。结果不应显示'i.com',即使它包含两个关键字但它们不是连续放置的。

当我输入'MICHAEL JACKSON THRILLER',时,结果应为

['mj.com']

因为3个单词'MICHAEL', 'JACKSON', 'THRILLER'分别连续放置在'mj.com',即位置(1,2,3)。

如果我输入'THRILLER JACKSON''THRILLER FEDERER',,结果应为NONE

3 个答案:

答案 0 :(得分:0)

使用集合和交集:

def func(key,dic):
    keys=key.split()
    values=[set(y if i==0 else y if dic[x][y]-dic[keys[i-1]].get(y,-10000)==1 \
                else None for y in dic[x]) for i,x in enumerate(keys)]
    sett=values[0]
    for y in values[1:]:
        sett &= y
    print(sett)

index = {'Michael': {'mj.com':1,'Nine.com':9,'i.com':34},
         'Jackson':{'One.com':4,'mj.com':2,'Nine.com':10,'i.com':45},
         'Thriller' : {'Seven.com':7,'Ten.com':10,'One.com':5,'mj.com':3}}

<强>输出:

>>> func("Michael",index)
{'Nine.com', 'mj.com', 'i.com'}
>>> func("Michael Jackson",index)
{'Nine.com', 'mj.com'}
>>> func("Michael Jackson Thriller",index)
{'mj.com'}

答案 1 :(得分:0)

作为旁注,CS的Udacity Intro恰好涵盖了这个问题。这对适当的输入做了许多假设(基本上它从未遇到任何不正确的输入)。

def lookup(index,KEYWORDS):
    kw = KEYWORDS.split()
    if len(kw) == 1:
        return [site[0] for site in index[kw[0]]]
    else:
        positions = {}
        result = []
        kw = KEYWORDS.split()
        for kword in kw:
            for site in index[kword]:
                positions[(kword,site[0])]=site[1]
        for i in range(0,len(kw)-1):
            cur_urls = [site[0] for site in index[kw[i]]]
            next_urls = [site[0] for site in index[kw[i+1]]]
            if i == 0:
                result = cur_urls
            for url in cur_urls:
                if url in next_urls:
                    if not (positions[kw[i+1],url]-positions[kw[i],url]) == 1:
                        result.remove(url)
                else:
                    if url in result:
                        result.remove(url)
        return result

答案 2 :(得分:0)

首先,由于您的搜索似乎忽略了大小写,因此您应该使用大写(或小写)索引。

index = dict((key.upper(), val) for (key, val) in index.iteritems())

现在这是一种递归方法。首先,界面功能:

def search(query):
    return [site for (site, pos) in search2(query.split())]

下一个功能完成实际工作。如果只给出一个单词,则返回该单词索引中的条目;否则过滤那些位置匹配的指数。

def search2(words):
    if len(words) == 1:
        return index[words[0]]
    else:
        return [(site1, pos1) for (site1, pos1) in index[words[0]]
                              for (site2, pos2) in search2(words[1:])
                              if (site1 == site2 and pos1 == pos2-1)]

当然,如果pos实际上可以成为每个网站的列表,事情会变得复杂一些......