Here are the instructions (please be patient, this is literally my 1st week trying to write VERY SIMPLE functions in Python). Every example I've found uses things my class hasn't used before.
"Define a function called longest_word(list_of_words)
, this function takes in a list of
words, i.e. ['apple', 'orange', 'banana']
and returns the the longest word. If there is more than one word tied for the longest, it will return the first.
a. For example, if you called the function as we do in the code below, you should get the output indicated:
word_list = [ 'apple' , 'orange' , 'banana' ]
result = longest_word(word_list)
print(result)"
So I tried this among many, many other failed attempts:
list_of_words = ['antman','spiderman','blackwidow','thor']
word_list_long = []
def longest_word(list_of_words):
for word in list_of_words:
if len(word) > len(word_list_long):
word.append(word_list)
len(word) != len(word_list_long)
return word_list
result = word_list_long
print(result)
And the result is
[]
, expected output is blackwidow
CodePudding user response:
The comments explain what each line is doing!
list_of_words = ['antman','spiderman','blackwidow','thor']
def longest_word(list_of_words):
longest = 0; #No Of Characters in longest word
longestword = "" #The Longest Word
for word in list_of_words: # For Loop to iterate through words
if len(word) > longest: #Check If the length of word is greater than current longest word
longest = len(word) #If So make the new longest length as length of the word
longestword = word # And make the longest word as the word
return longestword #Return The longest word
print(longest_word(list_of_words))
CodePudding user response:
I understand this is your first week programming and that this might be the wrong time to introduce this, but you can accomplish the same thing in one line with functools.reduce
>>> from functools import reduce
>>> l = ["1", "12", "123", "1234", "123", "12", "1"]
>>> reduce(lambda x, y: x if len(x) > len(y) else y, l)
'1234'
functools.reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x y, [1, 2, 3, 4, 5]) calculates ((((1 2) 3) 4) 5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned.
This call takes the list and compares two adjacent elements going from left to right, using the comparison function you provide. Here, I provide a simple lamdba
that returns whichever of the two is longer. By returning the longest of any two elements continually over the length of the list, I return the longest element in the list
CodePudding user response:
list_of_words = ['antman','spiderman','blackwidow','thor']
def longest_word(list_of_words):
longest_word = ''
for word in list_of_words:
if len(word) > len(longest_word):
longest_word = word
return (longest_word)
result = word_list_long(list_of_words)
print(result)
CodePudding user response:
word_list = ['apple', 'orange', 'banana']
def longest_word(word_list):
return max(word_list, key=len)
print(longest_word(word_list))
This should work, this 1 line function does everything you need.
CodePudding user response:
The following will find the longest word in the list and return it, or a list of them in the event of a tie:
def longest_words(words):
"""Return longest word or list of them if there's a tie."""
max_indices = []
max_len = len(words[0])
for i, length in ((i, len(word)) for i, word in enumerate(words) if len(word) >= max_len):
if length == max_len:
max_indices.append(i)
else:
max_len = length
max_indices = [i]
return words[max_indices[0]] if len(max_indices) < 2 else [words[i] for i in max_indices]
words = ['antman', 'spiderman', 'blackwidow', 'thor', 'submariner']
print(longest_words(words)) # -> ['blackwidow', 'submariner']