Python tup返回值

2024-09-29 23:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pyacoustid,我不明白为什么这段代码会起作用(艺术家实际上就是艺术家,依此类推):

first = True
        for score, rid, title, artist in self.fpresults:
            if first:
                first = False
            else:
                print
            print '%s - %s' % (artist, title)
            print 'http://musicbrainz.org/recording/%s' % rid
            print 'Score: %i%%' % (int(score * 100))

虽然这一块没有(当我打印时,它看起来是空的):

^{pr2}$

全班同学都来了(欢迎提出建议!)公司名称:

^{3}$

注意:acoustid.match(api_key, path)返回元组!在

编辑:

这个小例子

songfp = SongFP(sys.argv[1])
songfp.printResults()

{cd3>在哪里

class SongFP:
    """
    Song with FINGERPRINTS
    """
    fpresults = None

def __init__(self, path = None):
    """
    :param path: the path of the song
    """
    self.path = path
    try:
        self.fpresults = acoustid.match(api_key, path)
    except acoustid.NoBackendError:
        logger(paths['log'], "ERROR: chromaprint library/tool not found")
    except acoustid.FingerprintGenerationError:
        logger(paths['log'], "ERROR: fingerprint could not be calculated")
    except acoustid.WebServiceError, exc:
        logger(paths['log'], ("ERROR: web service request failed: %s" % exc.message))

def getFingerprintArtist(self):
        """
        Returns tuples with possible artists fetched from the MusicBrainz DB
        """
        return [artist for _, _, _, artist in self.fpresults]

def getFingerprintTitle(self):
    """
    Returns tuples with possible titles fetched from the MusicBrainz DB
    """
    return [title for _, _, title, _ in self.fpresults]

def getFingerprintID(self):
    """
    Returns tuples with IDs fetched from the MusicBrainz DB
    """
    return [rid for _, rid, _, _ in self.fpresults]

def getFingerprintScore(self):
    """
    Returns tuples with scores fetched from the MusicBrainz DB
    """
    return [score for score, _, _, _ in self.fpresults]

def printResults(self):
        print("Titles: %s" % self.getFingerprintTitle())
        print("Artists: %s" % self.getFingerprintArtist())
        print("IDs: %s" % self.getFingerprintID())
        print("Scores: %s" % self.getFingerprintScore())

当被调用为./app song.mp3时,只输出一些字段(如果一个字段为空,则所有其他字段也应如此,并且viceversa因为它获取在线MP3s元数据)

Titles: [u'Our Day Will Come', u'Our Day Will Come', u'Our Day Will Come', u'Our Day Will Come', u'Our Day Will Come']
Artists: []
IDs: []
Scores: []

日志中没有异常!


Tags: thepathinselffordefwithour
3条回答

如果它们只是元组,可以这样做:

def get_value_at_index(tuple_list, index):
    """Returns a list of the values at a given index."""
    return [tup[index] for tup in tuple_list]

[artist for score, rid, title, artist in self.fpresults]

你是唯一的商店艺术家。你需要这样的东西:

^{pr2}$

很难诊断这一点,但通常更常见的情况是这样分配不使用的变量:

def getFingerprintArtist(self):
    """
    Returns (list of)* possible artists fetched from the MusicBrainz DB
    """
    return  [artist for _, _, _, artist in self.fpresults]

你能把这个改写成一个最小的可重复的例子,这样我们就可以提供进一步的指导了吗?在

*这不是返回元组,它只是返回一个(从语义上讲)艺术家姓名列表!在


编辑-分析

我想这是因为你正在耗尽发电机的能量。在

^{pr2}$

在对象的实例化中填充一次,而在您的__init__中执行以下操作:

^{3}$

它将把生成器生成的信息作为属性保存在内存中,直到list或{}对象不再被引用,然后被垃圾回收。在

相关问题 更多 >

    热门问题