Simplest approach:
in [24]: a = [4, 34, 0, 0, 6, 34, 1]
In [25]: j=0
In [26]: M=[]
In [27]: m = max(a)
In [28]: for i in a:
if i==m:
M.append(j)
j+=1
....:
In [29]: M
Out[29]: [1, 5]
Using list-comprehension and enumerate, the above can be shortened to:
In [30]: [i for i, x in enumerate(a) if x == max(a)]
Out[30]: [1, 5]
Answer from Fredrik Pihl on Stack OverflowSimplest approach:
in [24]: a = [4, 34, 0, 0, 6, 34, 1]
In [25]: j=0
In [26]: M=[]
In [27]: m = max(a)
In [28]: for i in a:
if i==m:
M.append(j)
j+=1
....:
In [29]: M
Out[29]: [1, 5]
Using list-comprehension and enumerate, the above can be shortened to:
In [30]: [i for i, x in enumerate(a) if x == max(a)]
Out[30]: [1, 5]
A "fully manual" approach, using none of those pesky standard library functions:
def get_max_indices(vals):
maxval = None
index = 0
indices = []
while True:
try:
val = vals[index]
except IndexError:
return indices
else:
if maxval is None or val > maxval:
indices = [index]
maxval = val
elif val == maxval:
indices.append(index)
index = index + 1
What it loses in brevity, it gains in... not much.
dictionary - Using Python's max to return two equally large values - Stack Overflow
max - Which maximum does Python pick in the case of a tie? - Stack Overflow
multiple maximums in a list python - Stack Overflow
How do I find the relative maximum in a function?
Is max() an in-built function in Python?
What is the use of the max() and min() functions in Python?
What is the difference between the Python max() and min() functions?
Idea is to find max value and get all keys corresponding to that value:
count = {'a': 120, 'b': 120, 'c': 100}
highest = max(count.values())
print([k for k, v in count.items() if v == highest])
Same idea as Asterisk, but without iterating over the list twice. Bit more verbose.
count = { 'a': 120, 'b': 120, 'c': 100 }
answers = []
highest = -1
def f(x):
global highest, answers
if count[x] > highest:
highest = count[x]
answers = [x]
elif count[x] == highest:
answers.append(x)
map(f, count.keys())
print answers
It picks the first element it sees. See the documentation for max():
If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as
sorted(iterable, key=keyfunc, reverse=True)[0]andheapq.nlargest(1, iterable, key=keyfunc).
In the source code this is implemented in ./Python/bltinmodule.c by builtin_max, which wraps the more general min_max function.
min_max will iterate through the values and use PyObject_RichCompareBool to see if they are greater than the current value. If so, the greater value replaces it. Equal values will be skipped over.
The result is that the first maximum will be chosen in the case of a tie.
From empirical testing, it appears that max() and min() on a list will return the first in the list that matches the max()/min() in the event of a tie:
>>> test = [(1, "a"), (1, "b"), (2, "c"), (2, "d")]
>>> max(test, key=lambda x: x[0])
(2, 'c')
>>> test = [(1, "a"), (1, "b"), (2, "d"), (2, "c")]
>>> max(test, key=lambda x: x[0])
(2, 'd')
>>> min(test, key=lambda x: x[0])
(1, 'a')
>>> test = [(1, "b"), (1, "a"), (2, "d"), (2, "c")]
>>> min(test, key=lambda x: x[0])
(1, 'b')
And Jeremy's excellent sleuthing confirms that this is indeed the case.
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)
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']