How to find length of an element in a list?
I'm just starting with programming. I have a list of a few strings and now I need to print the biggest (in length) one. So I first want to just print the lengths of elements. I was trying things like this:
l = ("xxxxxxxxx", "yyyy","zz开发者_Python百科")
for i in range(len(l)):
So how do I do it?
l = ("xxxxxxxxx", "yyyy","zz")
print(max(l, key=len))
First of all you don't have a list, you have a tuple. this code will work for any sequence, however; both lists and tuples are sequences (as well as strings, sets, etc). So, the max
function takes a key
argument, that is used to sort the elements of an iterable. So, from all elements of l
will be selected the one having the maximum length.
To print the lengths of the elements:
elements = ["xxxxxx", "yyy", "z"]
for element in elements:
print len(element)
I recommend you read some tutorial material, for instance http://docs.python.org/tutorial/
For those who are here because they want to measure the lengths of all the elements in a sequence(list,tuple, etc) and return those lengths into another sequence (list, tuple etc), do this:
TLDR
list_of_lengths = (lambda x:[len(i) for i in x])(lst)
Longer explanation (from the inner brackets, moving outwards)
- We loop through our list and get the corresponding length value of each element using the len() function which is inbuilt into Python.
[len(i) for i in x]
- Create a temporary function to wrap the looping operation in the previous point so that any list can be put into the function and its element lengths returned.
(lambda x:[len(i) for i in x])
- Call the function using our list as the argument by putting it in brackets next to the function definition.
(lambda x:[len(i) for i in x])(lst)
- Assign the newly created list to variable so that you can use it for other operations (like finding the largest/smallest element or its index as seen in the question asked on this page.)
list_of_lengths = (lambda x:[len(i) for i in x])(lst)
>>> sorted(['longest','long','longer'],key=len)[-1]
'longest'
UPDATE: SilentGhost's solution is a lot nicer.
just ask for the max according to the length
print max(["qsf","qsqsdqsd","qs"], key = len)
The following code will print the largest string in the set:
l = ["abcdev", "xx","abcedeerer"]
len_0 = len(l[0])
pos = 0
for i in range(0,len(l)):
if len(l[i]) > len_0:
pos = i
print l[pos]
This code maps "len" function over the list, to get another list containing the length of every item.
mylist = ("xxxxxxxxx", "yyyy","zz")
len_mylist = map(len, mylist)
max_position = len_mylist.index(max(len_mylist))
my_item = mylist[max_position]
print('The largest item of my list is: ' + my_item)
print('located at position: ' + str(max_position))
First of all to find the length of strings you can define the function and use map function as below:
def len_words(l:list):
len_list = list(map(len,l))
return len_list
After that you can call the max function on len_words to find the maximum value of string from list.
max(len_words(l))
精彩评论