这是一个很长的问题,请耐心听我说。我从3个API中获得的3个dict开始。格言的结构如下:
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位置的分数列表:
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的fromScoredResults
与API1
、API2
或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},
...}
这对我来说太难了。如果你回顾一下我的问题历史,我已经问了很多字典问题,但到目前为止还没有实现。如果有人能给我指出正确的方向,我将不胜感激。你知道吗
快速尝试:
用给定的数据
导致:
我会把API转换成对你更有意义的东西。URL的dict可能更合适:
相关问题 更多 >
编程相关推荐