简体   繁体   中英

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")

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/

>>> sorted(['longest','long','longer'],key=len)[-1]
'longest'

UPDATE: SilentGhost's solution is a lot nicer.

只需根据长度要求最大值

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))

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)

  1. 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]
  1. 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])
  1. 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)
  1. 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)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM