I am trying to create a for loop for addition of a the variable associated with 'length' inside this list of dictionaries in a dictionary. I have searched online for answers, this site included to calculate this addition, but nothing quite matches. The assignment designates: This function should return a string, the name of the longest album.
Below is all the information for the albums, and what I have so far. Of course, my way does technically work. I became frustrated with the nested for loops, and couldn't figure out the correct format. I would like to find the correct way to format the for loop so I can get the same answer without using this repetitive way. `
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")
`
The for loop I began with started as such. I tried a lot of other combinations, but I deleted them many times previously, so I don't have every exact thing I tried:
`
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']
'
When I use a return to statement to see if there are any variables in album1 and album2, I see that they are both 0's still. I'm hoping this is a simple formatting issue. Thanks again.
CodePudding user response:
The issue with your 2nd approach is that your for
loop iterates over a list of dicts and i == 'length'
will just compare a dictionary structure with plain string: that's always False
.
Moreover, we need to also consider a case when two albums would have equal total length.
The optimal solution would be as follows:
def get_longest_album_name(alb_1, alb_2):
# summing up length of songs
alb_1_len = sum(s['length'] for s in alb_1['songs'])
alb_2_len = sum(s['length'] for s in alb_2['songs'])
if alb_1_len == alb_2_len:
print('Albums have equal length!')
return alb_1['name'], alb_2['name']
elif alb_1_len > alb_2_len:
return alb_1['name']
else:
return alb_2['name']
print(get_longest_album_name(album1, album2)) # Album2
CodePudding user response:
You can put the two albums in a list and then iterate through that list to avoid using two loops:
def longest_album(A1,A2):
albums = [A1, A2]
sum = [0, 0]
for i in range(2):
for song in albums[i]['songs']:
sum[i] = song['length']
if sum[0] > sum[1]:
return A1['name']
return A2['name']
CodePudding user response:
Would the below achieve the desired result?
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 album_length(album):
return sum(song["length"] for song in album["songs"])
def longest_album(*albums):
return sorted(albums, key=album_length, reverse=True)[0]["name"]
assert_equal(longest_album(album1,album2), "Album2")