列表中字典之间的匹配

Match between dictionaries in list

我对字典之间如何匹配有疑问

我向 youtube 发送了 3 个请求api

我尝试创建的是具有

的dic

在这里你可以看到代码和我发送的请求:

 def get(self,request):
    search_url = "https://www.googleapis.com/youtube/v3/search"
    video_url = "https://www.googleapis.com/youtube/v3/videos"
    channel_url = "https://www.googleapis.com/youtube/v3/channels?part=snippet&id='+commaSeperatedList+'&fields=items(id%2Csnippet%2Fthumbnails)&key={}".format(settings.YOUTUBE_DATA_API_KEY)


    para_search = {
      'part': 'snippet',
      'q': 'Learn Python' ,
      'key': settings.YOUTUBE_DATA_API_KEY,
      'maxResults': 3,
      'type': 'video'

    }

    search_response = requests.get(search_url,params=para_search)
    
    print(search_response.text)
    results = search_response.json()['items']
    ids =[]
    for result in results:
      ids.append(result['id']['videoId'])


    para_videos = {
      'part': 'snippet',
      'key': settings.YOUTUBE_DATA_API_KEY,
      'id':','.join(ids),
    }
    video_response = requests.get(video_url, params=para_videos)
    print(video_response.text)
    results = video_response.json()['items']
    dict_youtube  = {}
    list_youtube = []
    channelIdList = []
    for result in results:
      dict_youtube  = {
         'title': result['snippet']['title'],
         'thumbnails': result['snippet']['thumbnails']['high']['url'],
         'channelId': result['snippet']["channelId"],
       }
      channelIdList.append(result['snippet']["channelId"])
      list_youtube.append(dict_youtube)



    param_channel = {
      'part':'snippet,contentDetails,statistics',
      'key':settings.YOUTUBE_DATA_API_KEY,
      'id':','.join(channelIdList)
    }
    channel_response = requests.get(channel_url,params=param_channel)
    print(channel_response.text)
    results = channel_response.json()['items']
    profile = []
    profile_dic = {}
    for result in results:
      profile_dic = {
        'channelId': result['id'],
        'profile': result['snippet']['thumbnails']['default']['url'],
      }
      profile.append(profile_dic)
    print(profile)
    print(list_youtube)

输入:

profile = [{'channelId': 'UC8butISFwT-*******', 'profile': 'https://yt3.ggpht.com/ytc/A*******ifQn-nYNfkgLvVPkw=s88-********-no-rj'}, {'channelId': 'UCWv7*******mDpPBA', 'profile': 'https://yt3.ggpht.com/tBEPr-zTNXEeae7VZK******2PXSwzMBKVR7W0MI7gyND8=s88-c-k-c0x00ffffff-no-rj'}]

list_youtube = [{'title': 'Learn Python - Full Course for Beginners [Tutorial]', 'thumbnails': 'https://i.ytimg.com/vi/rf****bw/hqdefault.jpg', 'channelId': 'UC******wT-Wl7EV0hUK0BQ'}, {'title': 'Python for Beginners - Learn Python in 1 Hour', 'thumbnails': 'https://i.ytimg.com/vi/kqt*****8/hqd****t.jpg', 'channelId': 'UCWv7*********pPBA'}, {'title': 'Python Tutorial - Python Full Course for Beginners', 'thumbnails': 'https://i.ytimg.com/vi/_uQrJ0TkZlc/hqdefault.jpg', 'channelId': 'U********PBA'}]

如您所见,我创建了两个列表,每个列表都有字典。

并且每个字典都有一个我创建的公共键,它是 channelId

我想做的是在 channelId 键中具有相同值的字典之间的联合,第一个列表比第二个列表具有更少的字典。

如何合并两个列表和字典,使一切兼容 最终我会得到一个包含键

的字典的列表
  1. 'title':
  2. 'thumbnails':
  3. 'channelId':
  4. 'profile':

例如:

[{'title':.... , 'thumbnails':... , 'channelId':... , 'profile':... ,} , { ... }, ...]

这听起来像是经典的连接操作。您可以使用过滤器来识别按 id 匹配的元素,然后用它的值更新字典,就像这样。

这假设您的每个个人资料最多有 1 个视频。您可能需要根据它们的关系

翻转 variables/add 逻辑
for dic in profile:
    vids = filter(lambda yt: yt["channelId"] == dic["channelId"], list_youtube)
    for vid in vids:
        dic.update(vid)
    return dic