如果我使用last.fm API包装器查找pylast,并查找similar tracks,如下所示:

track = last.get_track('Radiohead', 'Karma Police')
    for similar in track.get_similar():
        print (similar)


我得到:

SimilarItem(item=pylast.Track(u'Radiohead', u'No Surprises', pylast.LastFMNetwork('key', 'secret', 'string', 'user', 'string2')), match=1.0)
SimilarItem(item=pylast.Track(u'Radiohead', u'Paranoid Android', pylast.LastFMNetwork('key', 'secret', 'string', 'user', 'string2')), match=0.995)


如果我尝试索引它:

track = last.get_track('Radiohead', 'Karma Police')
    for similar in track.get_similar():
        print (similar[0])


我得到:

Radiohead - No Surprises
Radiohead - Paranoid Android


我想以此方式打印similar

         {
         "artist": "Radiohead",
         "track":"No surprises"
         },
         {
         "artist": "Radiohead",
         "track":"No surprises"
        }


但是如果尝试similar[0][0],则会出现以下错误:

TypeError: 'Track' object does not support indexing

那么,如何将结果构造为dictionary

最佳答案

如果您想要字典,可以按照以下方式进行:

track = last.get_track('Radiohead', 'Karma Police')
d = dict(str(item[0]).split(" - ") for item in track.get_similar())

09-27 23:47