I have a problem with finding the longest word from list of words in python. I wrote code, but it said:
ValueError: Keyword arguments are not supported by this function on line 2
Code:
def find_longest_word(words_list):
longest_string = max(words_list, key=len)
print(longest_string)
Do you know why it doesn't work?
This will work for you
from itertools import count
def longwords(l, x):
c = count()
return sorted(l, key=lambda i: (len(i), next(c)),
reverse=True)[:x]
listA = ['hello','cat','boat','Earth','Moonshine','Aurora','Snowflakes','Sunshine']
n = 8
print(longwords(listA, n))
Change the n to find the descending order You can get more info about lambda in https://www.w3schools.com/python/python_lambda.asp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.