![](/img/trans.png)
[英]how to return a tuple from a dictionary that satisfy True value parameter in Python
[英]Python - Return a value from a tuple
我正在使用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))
虽然这个块没有(当我打印时它似乎是空的):
def getFingerprintArtist(self):
"""
Returns tuples with possible artists fetched from the MusicBrainz DB
"""
return [artist for score, rid, title, artist in self.fpresults]
这是全班(欢迎提出建议!):
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 setPath(self, path):
self.path = path
def printResults(self):
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))
def setFPResults(self):
self.fpresults = acoustid.match(api_key, self.path)
def getFingerprintArtist(self):
"""
Returns tuples with possible artists fetched from the MusicBrainz DB
"""
return [artist for score, rid, title, artist in self.fpresults]
def getFingerprintTitle(self):
"""
Returns tuples with possible titles fetched from the MusicBrainz DB
"""
return [title for score, rid, title, artist in self.fpresults]
def getFingerPrintID(self):
"""
Returns tuples with IDs fetched from the MusicBrainz DB
"""
return [rid for score, rid, title, artist in self.fpresults]
def getFingerPrintScore(self):
"""
Returns tuples with scores fetched from the MusicBrainz DB
"""
return [score for score, rid, title, artist in self.fpresults]
注意: acoustid.match(api_key, path)
返回元组!
编辑:
这个小例子
songfp = SongFP(sys.argv[1])
songfp.printResults()
SongFP
在哪里
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
时, ./app song.mp3
仅输出一个字段(如果一个字段为空,则其他字段也应该输出,反之亦然,因为它获取在线MP3的元数据)
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: []
日志中没有EXCEPTIONS!
这很难诊断,但是分配不使用的变量通常更常见:
def getFingerprintArtist(self):
"""
Returns (list of)* possible artists fetched from the MusicBrainz DB
"""
return [artist for _, _, _, artist in self.fpresults]
您可以将其重写为最小的可复制示例,以便我们提供进一步的指导吗?
*这不是返回元组,而只是返回(从语言上来说)艺术家姓名的列表!
编辑分析
我认为这里正在发生的事情是您正在耗尽发电机。
self.fpresults
在对象的实例化中填充一次,而是在__init__
执行以下操作:
try:
self.fpresults = list(acoustid.match(api_key, path))
它将生成器生成的信息作为属性保存在内存中,直到不再引用list
或SongFP
对象,然后进行垃圾回收。
如果它们只是元组,则可以这样:
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]
您只是商店的艺术家。 您需要类似:
[(artist, score, rid, title) for artist, score, rid, title in self.fpresults]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.