On 21/05/2019 14:30, Rahul Alawani wrote:

> # 4. The longest song title in the entire songs list
> def longest_title (songmix):
> temp = max(songmix, key=lambda num: len(max(num['titles'])))
> return list(map(lambda x: max(songmix, key=lambda num: 
> len(max(num['titles'])))['artist'],
>  filter(lambda title: max(len('title')), temp)))
> print (longest_title (songmix))

There are several problems here.
The first and the one that the error is reporting lies in the call to
max() max() expects a sequence of values and returns the biggest.
But you are passing len('title') which is a single integer value.
Hence the error about int not being iterable...

But there is another problem with len('title')
That will give the result 5 each time since its taking the length of the
string 'title'.
I suspect you wanted len(title) without the quotes?

Finally max() applied to strings returns the highest in lexical value -
ie 'b' is higher than 'a'... So

>>> max(['a','aa','aaaa','aaa','baa'])
'baa'
>>>

Which I don't think is what you want?

However, there is also a problem with the filter call.
filter expects a function that returns a boolean result. It then applies
that test to each member of its sequence parameter and returns a list
containing only those members for which the test was true.

In your case the lambda would return a number - the result of max.
All non zero numbers are treated as true by Python so you will get back
the same list you started with since all the max calls will (probably)
return >0 results.

Also I notice you are using key functions to max but that is hardly ever
necessary since max will return the largest item regardless of the
order. It only needs a key if there are multiple items of the same size
and you want to return a specific one of those. Sorting will ensure the
right value is picked. But in your case you don't need any sorting.

I know you want to practice lambdas but I think initially it would be
better for you to practice with map and filter using small named
functions. You can then convert them to lambdas later.

So for example(untested, so probably buggy):

def title_with_size(song): return (len(song), song)

def song_lengths(singer): return map(title_with_size, singer['titles'])

def max_title(singer): return max(song_lengths(singer))

def longest_title (songmix):
    titles = map(max_title, songmix)
    return max(titles)[1]

Removing the lambdas makes everything easier to read and therefore
easier to understand.

Once you understand how the map/filter etc work you can
decide where the named functions can be replaced by lambdas.
As it stands your code is a fine demonstration of why
lambdas are relatively rarely used!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to