0

Using for loop .. how can i chosse fbe longest name in the list ?

17th Dec 2016, 3:53 AM
amrr
2 Respostas
+ 2
Like this (assuming you're using python 2.7, but 3 wouldn't be much different): lis = ["uuuuuu", "iia", "oooo"] print max(lis, key=len) The "max()" function will return the maximum (aka longest/largest) item according to the key (in this case, len or length) so it will print out "uuuuuu" (without the quotes of course).
17th Dec 2016, 4:10 AM
Riley Johnson
Riley Johnson - avatar
+ 1
int longestIndex = 0; chars = 0; for(int i=0;i < arr.lenght ; i++){ if (arr[i].lenght>chars) chars = arr[i].lenght longestIndex=i; }
17th Dec 2016, 3:58 AM
Nahuel
Nahuel - avatar