1

Hey so being the NEWBY I am I was wondering how I could find multiple maximums (i.e. there is more than one maximum or item of the same length) and minimums(same situ as maximums) from a list. I've tried using the max function but yet it prints only one item, same with min. It is to be done for length of the string in the list (e.g. usinglen)!

this is the code i had so far

    def choice4(filelist):
       try:
           c4longest=max(filelist,key=len)
           print(c4longest,"is the longest item in the list")
1
  • The builtin will only give you the first maximum. If you want to return all of the maxima, you'll need to roll your own function for that (It should be pretty easy) Commented Jan 24, 2013 at 18:53

4 Answers 4

5

Try this:

def choice4(filelist):
    mymax = max(map(len,filelist))
    return [a for a in filelist if len(a)==mymax]

a = ['joe','andy','mark','steve']
a.extend(a)
print choice4(a)
Sign up to request clarification or add additional context in comments.

1 Comment

Let me know if you would like a revision on my posting to better serve your question. Thanks!
2

You could use sorting instead:

maxed = sorted(inputlist, key=lambda i: len(i), reverse=True)
allmax = list(takewhile(lambda e: len(e) == len(maxed[0]), maxed))

which takes O(n log n) time for the sort; but it's easy and short as the longest elements are all at the start for easy picking.

For a O(n) solution use a loop:

maxlist = []
maxlen = 0
for el in inputlist:
    l = len(el)
    if l > maxlen:
       maxlist = [el]
       maxlen = l
    elif l == maxlen:
       maxlist.append(el)

where maxlist is built and replaced as needed to hold only the longest elements:

>>> inputlist = 'And so we give a demo once more'.split()
>>> maxlist = []
>>> maxlen = 0
>>> for el in inputlist:
...     l = len(el)
...     if l > maxlen:
...        maxlist = [el]
...        maxlen = l
...     elif l == maxlen:
...        maxlist.append(el)
... 
>>> maxlist
['give', 'demo', 'once', 'more']

1 Comment

This makes me cringe a bit from an algorithmic standpoint -- Why use an O(nlogn) algorithm when you only need to have O(n)? -- That said, for moderate sized lists, this is quick, easy and effective.
0
In [1]: l = 'Is that what you mean'.split()

In [2]: [word for word in l if len(word) == max(map(len, l))]
Out[2]: ['that', 'what', 'mean']

2 Comments

But the O(n**2) could easily be cut down to O(n) by computing the max only once ... (after all, the result will be the same every time)
@MartijnPieters Well, yeah, but yours is O(n log n), and it really can be done in one loop. But meh.
0

use collections.Counter

from collections import Counter
d = Counter({k:v for k,v in enumerate(L)})
print d.most_common(5)  # to get top 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.