How to get all the maximums max function

2019-05-01 04:01发布

问题:

max('str','frt',key=len)

The max function returns only one of the string.
How can i make it return both of the strings?
The length of both strings is equal so max should return both the strings butit return only one so is there a way to return all max items.

回答1:

You can write this as a list comprehension:

data = ['str', 'frt']
maxlen = max(map(len, data))
result = [s for s in data if len(s) == maxlen]


回答2:

By definition, the max function returns the maximum value. It doesn't return the item, just the value which is unique (even if there are multiple item with the same max value). I suggest you use a sort algorithm and take whatever values you need.

In your example:

data = ['str','frt']
sorted(data,key=len, reverse=True)
result = [s for s in data if len(s)==len(data[0])]


回答3:

Here's a simple function which does this in one pass:

def maxes(a, key=None):
    if key is None:
        key = lambda x: x
    m, max_list = key(a[0]), []
    for s in a:
        k = key(s)
        if k > m:
            m, max_list = k, [s]
        elif k == m:
            max_list.append(s)
    return m, max_list

In action:

In [11]: maxes(['a', 'ab', 'a', 'cd'], key=len)
Out[11]: (2, ['ab', 'cd'])

This may, or may not be faster than running the list comprehension mentioned by the other poster and certainly faster than the sorting... but a little testing suggests its faster:

For a example of strings:

In [20]: a = [''.join(random.choice('abc') for _ in xrange(random.randint(1, 100)))
                                           for i in xrange(1000)]

In [21]: %timeit maxes(a, key=len)
10000 loops, best of 3: 53 µs per loop

In [22]: %timeit m = max(map(len, a)); [s for s in a if len(s) < m]
10000 loops, best of 3: 104 µs per loop

In [23]: %timeit sorted_a = sorted(a, key=len, reverse=True); [s for s in a if len(s) == len(sorted_a[0])]
1000 loops, best of 3: 322 µs per loop

If we look at integers, with a key:

In [30]: a = [random.randint(1, 10000) for i in xrange(1000)]

In [31]: %timeit maxes(a, key= lambda x: x**2)
10000 loops, best of 3: 150 µs per loop

In [32]: %timeit m = max(a, key=lambda x: x**2); [s for s in a if s**2 < m]
1000 loops, best of 3: 183 µs per loop

In [33]: %timeit sorted_a = sorted(a, key=lambda x: x**2, reverse=True); [s for s in a if s ** 2 == sorted_a[0] ** 2]
1000 loops, best of 3: 441 µs per loop

However, without a key the list comprehension is better:

In [34]: %timeit maxes(a)
10000 loops, best of 3: 98.1 µs per loop

In [35]: %timeit m = max(a); [s for s in a if s < m]
10000 loops, best of 3: 49.2 µs per loop

In [36]: %timeit sorted_a = sorted(a, reverse=True); [s for s in a if s == sorted_a[0]]
10000 loops, best of 3: 152 µs per loop

This is expected since the redundant key code is still being applied, if we were toremove that logic (replace calls to key(x) with just x) the function is again slightly faster:

In [37]: %timeit maxes2(a)
10000 loops, best of 3: 39.7 µs per loop


标签: python max