이것은 긴 질문이므로 저와 함께주십시오. 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 키를 추출하십시오 dicts는 구조과 같이 있습니다. 이 작업을 수행하려면 상당히 많은 처리가 필요하고 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]
내가 사용하여 이러한 2 목록에서 새 사전을 생성 할 것을 zip()
기능.
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},
...}
이는 다음과 같습니다
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}
지금 내가해야 할 일을 새 사전과 같이 내가 가진 너무 API1
, API2
또는 API3
와 ScoredResults
에서 URL의를 연결하는 것입니다 내가하기에는 너무 어렵다. 내 질문에 대한 역사를 되돌아 보면 수많은 사전 질문을하고 있지만 지금까지는 아무런 구현도 없었다. 누구든지 올바른 방향으로 나를 가리켜 주시면 감사하겠습니다. 주어진 데이터와
다양한 API에서 반환 된 결과가 고유하다는 것을 보장 할 수 있습니까? 예를 들어, API2와 API3 모두에서 'www.dol.com'을 URL로 사용합니다. 실제 코드에서 실제로 가능합니까? 그렇다면 URL 만 지정하면 API2 또는 API3에서 온 것인지 여부를 알 수 없습니다. – mgilson
@mgilson 예 중복은 URL에서 가능하지만 제목/스 니펫의 모든 버전에 만족합니다. 'API1의 url1이 제목 및 스 니펫을 가져오고, API2의 elif가 제목 및 스 니펫을 가져 오는 경우 ...' – adohertyd
여러 API에 의해 반환 된 사전은 정말 간단합니까? (즉, 항상 하나의 키/값 쌍만 있습니까?) – mgilson