如何用for循环计算嵌套字典列表中length总和以找出最长专辑
问题:用循环优化专辑总时长计算函数
我需要实现一个函数,返回总时长最长的专辑名称。目前手动累加专辑歌曲列表中各字典的length值的方法可正常运行,但代码冗余,希望改用for循环优化。尝试编写的循环代码无法正确累加时长,album1和album2的总和始终为0,求正确的for循环写法。
可行但冗余的代码
from cisc108 import assert_equal album1={ 'name':'Album1', 'songs':[{'title':'rock','explicit':True,'length':2.5}, {'title':'starlight','explicit':False,'length':5.2}, {'title':'smile','explicit':False,'length':1.75}] } album2={ 'name':'Album2', 'songs':[{'title':'stone','explicit':False,'length':4.75}, {'title':'moonlight','explicit':False,'length':3.0}, {'title':'happy','explicit':True,'length':2.8}]} def longest_album(A1,A2): x1 = A1['songs'][0]['length'] x2 = A1['songs'][1]['length'] x3 = A1['songs'][2]['length'] album_1_len= x1 + x2 + x3 y1 = A2['songs'][0]['length'] y2 = A2['songs'][1]['length'] y3 = A2['songs'][2]['length'] album_2_len = y1 + y2 + y3 if album_2_len > album_1_len: return A2['name'] else: return A1['name'] assert_equal(longest_album(album1,album2), "Album2")
尝试的错误循环代码
def longest_album(A1,A2): album1 = 0 album2 = 0 for i in A1['songs']: if i == 'length': album1+= i for i in A2['songs']: if i == 'length': album2+= i if album2 > album1: return A2['name'] else: return A1['name']
错误原因分析
遍历A1['songs']时,变量i拿到的是每首歌的完整字典对象,不是字典的键。你用i == 'length'做判断,永远不会成立,所以累加逻辑从未执行,总和始终为0。
正确的for循环实现
from cisc108 import assert_equal album1={ 'name':'Album1', 'songs':[{'title':'rock','explicit':True,'length':2.5}, {'title':'starlight','explicit':False,'length':5.2}, {'title':'smile','explicit':False,'length':1.75}] } album2={ 'name':'Album2', 'songs':[{'title':'stone','explicit':False,'length':4.75}, {'title':'moonlight','explicit':False,'length':3.0}, {'title':'happy','explicit':True,'length':2.8}]} def longest_album(A1,A2): album1_total = 0 # 遍历专辑1的每首歌字典,直接取length值累加 for song in A1['songs']: album1_total += song['length'] album2_total = 0 # 遍历专辑2的每首歌字典,直接取length值累加 for song in A2['songs']: album2_total += song['length'] return A2['name'] if album2_total > album1_total else A1['name'] assert_equal(longest_album(album1,album2), "Album2")
进一步优化:抽离时长计算函数
为避免重复代码,可将单张专辑的时长计算逻辑抽成独立函数,让代码更易维护:
from cisc108 import assert_equal album1={ 'name':'Album1', 'songs':[{'title':'rock','explicit':True,'length':2.5}, {'title':'starlight','explicit':False,'length':5.2}, {'title':'smile','explicit':False,'length':1.75}] } album2={ 'name':'Album2', 'songs':[{'title':'stone','explicit':False,'length':4.75}, {'title':'moonlight','explicit':False,'length':3.0}, {'title':'happy','explicit':True,'length':2.8}]} def calculate_album_length(album): total = 0 for song in album['songs']: total += song['length'] return total def longest_album(A1,A2): len1 = calculate_album_length(A1) len2 = calculate_album_length(A2) return A2['name'] if len2 > len1 else A1['name'] assert_equal(longest_album(album1,album2), "Album2")
极简写法:用生成器表达式求和
Python的sum()函数配合生成器表达式,可一行完成时长计算,代码更简洁:
from cisc108 import assert_equal album1={ 'name':'Album1', 'songs':[{'title':'rock','explicit':True,'length':2.5}, {'title':'starlight','explicit':False,'length':5.2}, {'title':'smile','explicit':False,'length':1.75}] } album2={ 'name':'Album2', 'songs':[{'title':'stone','explicit':False,'length':4.75}, {'title':'moonlight','explicit':False,'length':3.0}, {'title':'happy','explicit':True,'length':2.8}]} def longest_album(A1,A2): len1 = sum(song['length'] for song in A1['songs']) len2 = sum(song['length'] for song in A2['songs']) return A2['name'] if len2 > len1 else A1['name'] assert_equal(longest_album(album1,album2), "Album2")
内容的提问来源于stack exchange,提问作者baylee
相关产品推荐
相关产品推荐

