+ 1
How to use for loop that appends to list items
Hello. I've had this problem in PHP also. I want to create a for loop that will iterate over so many split words in a list and for each of the items the loop will add an item to another list based on the original item being iterated over. I don't know why I suffer with this concept so much....
11 Respuestas
+ 2
** Read below edit for correction ***
Apparently, it even works without using a specific `key` argument :D
#your code goes here
words = txt.split() # make a list of words
print( max( words ) ) # <- WRONG RESULT
(Edit)
Apologies ρү૨œdԌ૨ × I was awfully wrong ...
It was a mistake. To get an actual longest word, the use of specific function to do the measuring (by specifying `key` argument for max() function) is necessary. See below test for why it is so ...
# different result of max() with/without `key` argument specifics
txt = "Sololearn has a community of more than 40 million coding students worldwide, ready to help you solve problems and discuss interesting concepts. The community creates new content to help you practice on a daily basis, this_is_the_longest_word helping new and old users alike to become better programmers."
words = txt.split(' ')
print(max(words), max(words, key = len), sep = "\n")
+ 4
Not getting the idea. Can you illustrate the data of the lists involved for better understanding?
+ 3
I see Rik Wittkopp had suggested you a workaround, so I guess the problem is solved?
+ 3
In general yes Ipang
+ 3
Alright, good job! 👍
+ 3
You don't have to do that. You can find the longest word by using max() function, passing `len` function reference for the `key` argument. The `key` parameter accepts a function reference which will be called by max() for each of its element, to measure which element was considered having the largest point. In this case, an element having the largest count of character.
#your code goes here
words = txt.split() # make a list of words
longest_word = max( words, key = len ) # use built-in max() function to find the longest word
print( longest_word, len( longest_word ) )
+ 2
Ipang https://code.sololearn.com/cnp0WQ4H0xj3/?ref=app
Ive had the same issue with foreach loops in PHP. How do I logically use the iterated variable in an iterating loop to also append to a list/array with each iteration.
+ 2
I am still trying to figure out how to attribute the index in the list of integers to the same list of words.
+ 2
Super! Wow your last one??! Cool! Check my code again haha I went the 'we enjoy typing' route lol
+ 2
Ipang oh you are correct. I will adjust my code example thanks. I mistaken as I wrote my input to have the longest word first. Still interesting that there are more arguments to the max() function. I have a hard time with standard functions as I feel like I would have to filter through documentation to read up on a functions total abilities. I dont have that programming discipline yet but it would be nice if I did.
+ 2
Ipang https://code.sololearn.com/c7pPBEDA2xSm/?ref=app
It is done aha! Thanks for enlightening me with this interesting way to use pythons.