首页 > 解决方案 > 如何从以下输出在python中获得所需的输出

问题描述

我得到这个输出,粘贴在下面。

[{'accel-world-infinite-burst-2016': 'https://yts.mx/torrent/download/92E58C7C69D015DA528D8D7F22844BF49D702DFC'}, {'accel-world-infinite-burst-2016': 'https://yts.mx/torrent/download/3086E306E7CB623F377B6F99261F82CC8BB57115'}, {'accel-world-infinite-burst-2016': 'https://yifysubtitles.org/movie-imdb/tt5923132'}, {'anna-to-the-infinite-power-1983': 'https://yts.mx/torrent/download/E92B664EE87663D7E5EC8E9FEED574C586A95A62'}, {'anna-to-the-infinite-power-1983': 'https://yts.mx/torrent/download/4F6F194996AC29924DB7596FB646C368C4E4224B'}, {'anna-to-the-infinite-power-1983': 'https://yts.mx/movies/anna-to-the-infinite-power-1983/request-subtitle'}, {'infinite-2021': 'https://yts.mx/torrent/download/304DB2FEC8901E996B066B74E5D5C010D2F818B4'}, {'infinite-2021': 'https://yts.mx/torrent/download/1320D6D3B332399B2F4865F36823731ABD1444C0'}, {'infinite-2021': 'https://yts.mx/torrent/download/45821E5B2E339382E7EAEFB2D89967BB2C9835F6'}, {'infinite-2021': 'https://yifysubtitles.org/movie-imdb/tt6654210'}, {'infinite-potential-the-life-ideas-of-david-bohm-2020': 'https://yts.mx/torrent/download/47EB04FBC7DC37358F86A5BFC115A0361F019B5B'}, {'infinite-potential-the-life-ideas-of-david-bohm-2020': 'https://yts.mx/torrent/download/88223BEAA09D0A3D8FB7EEA62BA9C5EB5FDE9282'}, {'infinite-potential-the-life-ideas-of-david-bohm-2020': 'https://yts.mx/movies/infinite-potential-the-life-ideas-of-david-bohm-2020/request-subtitle'}, {'the-infinite-man-2014': 'https://yts.mx/torrent/download/0E2ACFF422AF4F62877F59EAE4EF93C0B3623828'}, {'the-infinite-man-2014': 'https://yts.mx/torrent/download/52437F80F6BDB6FD326A179FC8A63003832F5896'}, {'the-infinite-man-2014': 'https://yifysubtitles.org/movie-imdb/tt2553424'}, {'nick-and-norahs-infinite-playlist-2008': 'https://yts.mx/torrent/download/DA101D139EE3668EEC9EC5B855B446A39C6C5681'}, {'nick-and-norahs-infinite-playlist-2008': 'https://yts.mx/torrent/download/8759CD554E8BB6CFFCFCE529230252AC3A22D4D4'}, {'nick-and-norahs-infinite-playlist-2008': 'https://yifysubtitles.org/movie-imdb/tt0981227'}]

如您所见,每部电影都有多个链接,并且每个链接的电影名称都在重复。我希望与同一部电影相关的所有链接都必须显示为相同的对象,例如

[{accel-world-infinite-burst-2016:{link1,link2,link3,link4},........]

for item in li:
            # print(item.partition("movies/")[2])
                movieName["Movies"].append(item.partition("movies/")[2])
                
                req=requests.get(item)
                s=soup(req.text,"html.parser")
                
                m=s.find_all("p",{"class":"hidden-xs hidden-sm"})
                # print(m[0])
                for a in m[0].find_all('a', href=True):
                    #  movieName['Movies'][item.partition("movies/")[2]]=(a['href'])
                     downloadLinks.append ( {item.partition("movies/")[2]:a['href']  })

标签: pythondictionarydata-structures

解决方案


你可以试试这个

# input = your list of dict
otp_dict = {}
for l in input:
  for key, value in l.items():
    if key not in otp_dict:
      otp_dict[key] = list([value])
    else:
      otp_dict[key].append(value)

print(otp_dict)

OTP:{'accel-world-infinite-burst-2016':[link1,link2],...}

输出是包含链接列表的dict,如果你想按照你在你想要的操作中提到的那样设置试试这个

for l in input:
  for key, value in l.items():
    if key not in otp_dict:
      otp_dict[key] = set([value])
    else:
      otp_dict[key].add(value)

OTP:{'accel-world-infinite-burst-2016':{link1,link2},...}


推荐阅读