1

这是一个很长的问题,所以请多多包涵。我从 3 个 API 获得的 3 个字典开始。dicts 的结构如下:

API1 = {'results':[{'url':'www.site.com','title':'A great site','snippet':'This is a great site'},
{'url':'www.othersite.com','title':'Another site','snippet':'This is another site'},
{'url':'www.wiki.com','title':'A wiki site','snippet':'This is a wiki site'}]}

API2 = {'hits':[{'url':'www.dol.com','title':'The DOL site','snippet':'This is the dol site'},
{'url':'www.othersite.com','title':'Another site','snippet':'This is another site'},
{'url':'www.whatever.com','title':'Whatever site','snippet':'This is a site about whatever'}]}

API3 = {'output':[{'url':'www.dol.com','title':'The DOL site','snippet':'This is the dol site'},
{'url':'www.whatever.com','title':'Whatever site','snippet':'This is a site about whatever'},
{'url':'www.wiki.com','title':'A wiki site','snippet':'This is a wiki site'}]}

我从 API1、API2 和 API3 中提取 URL 密钥以进行一些处理。我这样做是因为有相当多的处理要做,而且只需要 URL。完成后,我有一个删除重复项的 URL 列表和另一个与每个 URL 在列表中的位置相关的分数列表:

URLlist = ['www.site.com','www.wiki.com','www.othersite.com','www.dol.com','www.whatever.com']

Results = [1.2, 6.5, 3.5, 2.1, 4.0]

我所做的是使用该zip()函数从这两个列表中创建一个新字典。

ScoredResults = dict(zip(URLlist,Results))

{'www.site.com':1.2,'www.wiki.com':6.5, 'www.othersite.com':3.5, 'www.dol.com':2.1, 'www.whatever.com':4.0}

现在我需要做的是将 URL 与 链接ScoredResults起来API1API2或者API3这样我就有一个像这样的新字典:

Full Results = 
{'www.site.com':{'title':'A great site','snippet':'This is a great site','score':1.2},
 'www.othersite.com':{'title':'Another site','snippet':'This is another site','score':3.5},
...}

这对我来说太难了。如果你回顾我的问题历史,我一直在问很多字典问题,但到目前为止还没有实施。如果有人能指出我正确的方向,我将不胜感激。

4

4 回答 4

1

这样的东西对你有用吗?这是相当基本的,通过循环构建你的最终字典URLlist

API1r = API1['results']
API2r = API2['hits']
API3r = API3['output']

FullResults = {}
for (U, R) in zip(URLlist, Results):
    FullResults[U] = {}
    for api in (API1r, API2r, API3r):
        for v in api:
            k = dict()
            k.update(v)
            if (k.pop('url') == U):
                FullResults[U].update((k.items()+[('score', R)]))

请注意,由于url您的不同s中可能存在相同API但具有不同信息的信息,因此我们需要FullResults预先创建相应的条目,因此简化循环可能会有些棘手。LMKHIW。

于 2012-07-17T13:28:05.937 回答
1

快速尝试:

from itertools import chain

full_result = {}

for blah in chain.from_iterable(d.itervalues() for d in (API1, API2, API3)):
    for d in blah:
        full_result[d['url']] = {
            'title': d['title'],
            'snippet': d['snippet'],
            'score': ScoredResults[d['url']]
        }

print full_result
于 2012-07-17T13:00:27.923 回答
1

我会将 API 转换为对您更有意义的东西。url 的字典可能更合适:

def transform_API(API):
    list_of_dict=API.get('results',API.get('hits',API.get('output')))
    if(list_of_dict is None):
       raise KeyError("results, hits or output not in API")
    d={}
    for dct in list_of_dict:
        d[dct['url']]=dct
        dct.pop('url')
    return d

API1=transform_API(API1)
API2=transform_API(API2)
API3=transform_API(API3)

master={}
for d in (API1,API2,API3):
    master.update(d)

urls=list(master.keys())
scores=get_scores_from_urls(urls)

for k,score in zip(urls,scores):
    master[k]['score']=score
于 2012-07-17T13:11:49.527 回答
1

有了给定的数据……</p>

Full_Results = {d['url']: {'title': d['title'], 'snippet': d['snippet'], 'score': ScoredResults[d['url']]} for d in API1['results']+API2['hits']+API3['output']}

导致:

{'www.dol.com': {'score': 2.1,
  'snippet': 'This is the dol site',
  'title': 'The DOL site'},
 'www.othersite.com': {'score': 3.5,
  'snippet': 'This is another site',
  'title': 'Another site'},
 'www.site.com': {'score': 1.2,
  'snippet': 'This is a great site',
  'title': 'A great site'},
 'www.whatever.com': {'score': 4.0,
  'snippet': 'This is a site about whatever',
  'title': 'Whatever site'},
 'www.wiki.com': {'score': 6.5,
  'snippet': 'This is a wiki site',
  'title': 'A wiki site'}}
于 2012-07-17T12:54:34.930 回答