python - 如何使用 pylast 获取最近听过的 4 首轨道?

标签 python

我正在尝试使用 pylast 从 last_fm 获取我最近听过的 4 首歌曲。

到目前为止,我有这个代码,但操作系统只返回一首歌曲:

def get_recents(self, max_results):
        recents = self.user.get_recent_tracks(max_results)
        for song in recents:
            return str(song.track)

最佳答案

return 语句将立即跳出循环,这意味着它只会执行一次。只需使用列表理解返回歌曲列表:

def get_recents(self, max_results):
    recents = self.user.get_recent_tracks(max_results)
    return [str(x) for x in recents[:4]]

关于python - 如何使用 pylast 获取最近听过的 4 首轨道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53366407/

相关文章:

python - 如何将 df 保存到多个位置的两个 Excel 文件中?

python - 用Django进行复杂查询(来自各位小伙伴的帖子)

python - 诊断 boto3 中的内存泄漏

python - 使用 pyOpenSSL 从证书或其他连接信息中提取公钥

python - Mechanize 错误 - Python