But I still need my shot of Python now and then. Often I end up just doing small non useful snippets like list comprehensions stuff. I just love them, they are so beautiful and they make me happy :)
>>> # Find the longest words >>> words = "Some random words in a text string" >>> max([(len(w), w) for w in words.split()], key=lambda x:x[0])[1]
Simplified:
ReplyDeletemax((w for w in words.split()), key=len)
Sorry, it's 3:15 AM and I should be sleeping.
ReplyDeleteEven more simplified: max(words.split(), key=len)
You nailed it! This is why Python is so fun... You often end up doing much more readable code when "optimizing"
ReplyDeleteBefore max() was extended with the key argument, one might have used sorted(words.split(), key=len)[-1]
ReplyDeleteaa NICE ANY WAY
ReplyDelete