问题:Python选择列表中最长字符串的最有效方法?

我有一个可变长度的列表,正在尝试寻找一种方法来测试当前正在评估的列表项是否是列表中包含的最长字符串。我正在使用Python 2.6.1

例如:

mylist = ['abc','abcdef','abcd']

for each in mylist:
    if condition1:
        do_something()
    elif ___________________: #else if each is the longest string contained in mylist:
        do_something_else()

当然,有一个简单的列表理解功能很简短,但我却忽略了它?

I have a list of variable length and am trying to find a way to test if the list item currently being evaluated is the longest string contained in the list. And I am using Python 2.6.1

For example:

mylist = ['abc','abcdef','abcd']

for each in mylist:
    if condition1:
        do_something()
    elif ___________________: #else if each is the longest string contained in mylist:
        do_something_else()

Surely there’s a simple list comprehension that’s short and elegant that I’m overlooking?


回答 0

Python文档本身,您可以使用max

>>> mylist = ['123','123456','1234']
>>> print max(mylist, key=len)
123456

From the Python documentation itself, you can use max:

>>> mylist = ['123','123456','1234']
>>> print max(mylist, key=len)
123456

回答 1

如果最长的字符串超过1个(应该考虑’12’和’01’),该怎么办?

尝试获得最长的元素

max_length,longest_element = max([(len(x),x) for x in ('a','b','aa')])

然后定期进行foreach

for st in mylist:
    if len(st)==max_length:...

What should happen if there are more than 1 longest string (think ’12’, and ’01’)?

Try that to get the longest element

max_length,longest_element = max([(len(x),x) for x in ('a','b','aa')])

And then regular foreach

for st in mylist:
    if len(st)==max_length:...

回答 2

def longestWord(some_list): 
    count = 0    #You set the count to 0
    for i in some_list: # Go through the whole list
        if len(i) > count: #Checking for the longest word(string)
            count = len(i)
            word = i
    return ("the longest string is " + word)

或更容易:

max(some_list , key = len)
def longestWord(some_list): 
    count = 0    #You set the count to 0
    for i in some_list: # Go through the whole list
        if len(i) > count: #Checking for the longest word(string)
            count = len(i)
            word = i
    return ("the longest string is " + word)

or much easier:

max(some_list , key = len)

回答 3

要获取列表中最小或最大的项目,请使用内置的min和max函数:

lo = min(L)
hi = max(L)

与sort一样,您可以传入“ key”参数,该参数用于在比较列表项之前映射它们:

lo = min(L, key=int)
hi = max(L, key=int)

http://effbot.org/zone/python-list.htm

如果您正确地将其映射为字符串并将其用作比较,则看起来可以使用max函数。我建议当然只查找一次最大值,而不是列表中的每个元素。

To get the smallest or largest item in a list, use the built-in min and max functions:

lo = min(L)
hi = max(L)

As with sort, you can pass in a “key” argument that is used to map the list items before they are compared:

lo = min(L, key=int)
hi = max(L, key=int)

http://effbot.org/zone/python-list.htm

Looks like you could use the max function if you map it correctly for strings and use that as the comparison. I would recommend just finding the max once though of course, not for each element in the list.


回答 4

len(each) == max(len(x) for x in myList) 要不就 each == max(myList, key=len)

len(each) == max(len(x) for x in myList) or just each == max(myList, key=len)


回答 5

def LongestEntry(lstName):
  totalEntries = len(lstName)
  currentEntry = 0
  longestLength = 0
  while currentEntry < totalEntries:
    thisEntry = len(str(lstName[currentEntry]))
    if int(thisEntry) > int(longestLength):
      longestLength = thisEntry
      longestEntry = currentEntry
    currentEntry += 1
  return longestLength
def LongestEntry(lstName):
  totalEntries = len(lstName)
  currentEntry = 0
  longestLength = 0
  while currentEntry < totalEntries:
    thisEntry = len(str(lstName[currentEntry]))
    if int(thisEntry) > int(longestLength):
      longestLength = thisEntry
      longestEntry = currentEntry
    currentEntry += 1
  return longestLength

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。