get minimum and maximum values from a 'for in' loop - python
First post & I've probably got no business being here, but here goes...
How do I find the maximum and minimum values from the output of a 'for in' loop?
I've tried the min() and max() and get the following error...
TypeError: 'int' object is not iterable
here's my code...
import urllib2
import json
def printResults(data):
# Use the json module to load the string data into a dictionary
theJSON = json.loads(data)
# test bed for accessing the data
for i in theJSON["features"]:
t = i["properties"]["time"]
print t
def main():
# define a variable to hold the source URL
urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
# Open the URL and read the data
webUrl = urllib2.urlopen(urlData)
#print webUrl.getcode()
if (webUrl.getcode() == 200):
data = webUrl.read()
# print out our customized results
printResults(data)
else:
print "Received an error from server, cannot retrieve results " + str(webUrl.getcode())
if __name__ == "__main__":
main()
Any pointers will be greatly appreciated!
You can use min and max on iterables. Since you are looping through theJSON["features"], you can use:
print min(e["properties"]["time"] for e in theJSON["features"])
print max(e["properties"]["time"] for e in theJSON["features"])
You can also store the result in a variable, so you can use it later:
my_min = min(...)
my_max = max(...)
As #Sabyasachi commented you can also use:
print min(theJSON["features"], key = lambda x:x["properties"]["time"])
Here is an example of how you can manually keep track of a min and max.
minVal = 0
maxVal = 0
for i in yourJsonThingy:
if i < minVal:
minVal = i
if i > maxVal:
maxVal = i
You can't do this:
for i in yourJsonThingy:
maxVal = max(i)
Because i is just an integer and doesn't have a max
But you can perform those operations on a list of ints
maxVal = max(yourJsonThingy)
minVal = min(yourJsonThingy)
In the case that you only want to go through your iterable once, (say it's an expensive operation to do, and really that's the only reason you should do this, instead of doing max or min separately, but that said, the below is a performance improvement on calling both separately, see numbers below):
def max_min(iterable, key=None):
'''
returns a tuple of the max, min of iterable, optional function key
tuple items are None if iterable is of length 0
'''
it = iter(iterable)
_max = _min = next(it, None)
if key is None:
for i in it:
if i > _max:
_max = i
elif i < _min:
_min = i
else:
_max_key = _min_key = key(_max)
for i in it:
key_i = key(i)
if key_i > _max_key:
_max, _max_key = i, key_i
elif key_i < _min_key:
_min, _min_key = i, key_i
return _max, _min
usage:
>>> max_min(range(100))
(99, 0)
>>> max_min(range(100), key=lambda x: -x)
(0, 99)
To performance check:
>>> timeit.timeit('max(range(1000)), min(range(1000))', setup=setup)
70.95577674100059
>>> timeit.timeit('max_min(range(1000))', setup=setup)
65.00369232000958
Which is about a 9% improvement on calling both builtins, max and min, without a lambda, separately. With a lambda:
>>> timeit.timeit('max(range(1000), key=lambda x: -x),min(range(1000), key=lambda x: -x)', setup=setup)
294.17539755300095
>>> timeit.timeit('max_min(range(1000), key=lambda x: -x)', setup=setup)
208.95339999899443
Which is a more than 40% improvement on calling each separately with lambdas.
Related
Trying to remove list element if it's less than threshold?
I have a list: lst = ['aaaaabbbbb','yyyyaaaaaaxxx', 'eeeaaaaassaaa'] and I'm trying to see if between 50 to 60 percent of the list index consists of 'a'. If it doesn't, the index should be removed. Here's what I got: def remove_stuff(sequences, min_c, max_c): seq = cleanUp(display(sequences)) # used from another function that makes the list for i in seq: a = 'a' a_find = int(seq.count(a)) length_seq = int(len(seq)) average = ((a_find) / length_seq) * 100 if average < min_c: seq.remove(i) elif average > max_c: seq.remove(i) else: pass Output would be something like: ['aaaaabbbbb'] # only one that satisfied between 50-60% How could I go about getting the new modified list? Thanks for the help!
In [2]: lst = ['aaaaabbbbb','yyyyaaaaaaxxx', 'eeeaaaaassaaa'] In [3]: lst = [i for i in lst if 0.5<i.count('a')/len(i)<0.6] In [4]: lst Out[4]: ['aaaaabbbbb']
You can use a filter for that: def condition_a(elem): count_of_a = elem.count('a') rate_of_a = count_of_a / len(elem) return 0.5 <= rate_of_a <= 0.6 lst = ['aaaaabbbbb','yyyyaaaaaaxxx', 'eeeaaaaassaaa'] list(filter(condition_a, lst))
You could define a method to keep elements based on condition (predicate): def keep_if(predicate, iterable): for element in iterable: if predicate(element): yield element Then define a method to test the condition: def is_between_50_60_percent(letter, string): perc = string.count(letter) / len(string) return perc >= 0.5 and perc <= 0.6 Given your list: lst = ['aaaaabbbbb','yyyyaaaaaaxxx', 'eeeaaaaassaaa'] Finally you can just call: keep_if( lambda x: is_between_50_60_percent('a', x), lst) res = keep_if( lambda x: is_between_50_60_percent('a', x), lst) print(list(res)) #=> ['aaaaabbbbb']
Avoid lexicographic ordering of numerical values with Python min() max()
I have a script to pull random numbers from a set of values. However, it broke today because min() and max() sort values by lexicographic order (so 200 is considered greater than 10000). How can I avoid lexicographic order here? Len key is on the right track but not quite right. I couldn't find any other key(s) that would help. data_set = 1600.csv, 2405.csv, 6800.csv, 10000.csv, 21005.csv First try: highest_value = os.path.splitext(max(data_set))[0] lowest_value = os.path.splitext(min(data_set))[0] returns: lowest_value = 10000 highest_value = 6800 Second try: highest_value = os.path.splitext(max(data_set,key=len))[0] lowest_value = os.path.splitext(min(data_set,key=len))[0] returns: lowest_value = 1600 highest_value = 10000 Thanks.
You can use key to order by the numeric part of the file: data_set = ['1600.csv', '2405.csv', '6800.csv', '10000.csv', '21005.csv'] highest = max(data_set, key=lambda x: int(x.split('.')[0])) lowest = min(data_set, key=lambda x: int(x.split('.')[0])) print(highest) # >> 21005.csv print(lowest) # >> 1600.csv
You were close. Rather than using the result of splittext with the len function, use the int function instead: >>> from os.path import splitext >>> data_set = ['1600.csv', '2405.csv', '6800.csv', '10000.csv', '21005.csv'] >>> def convert_to_int(file_name): return int(splitext(file_name)[0]) >>> min(data_set, key=convert_to_int) '1600.csv' >>> max(data_set, key=convert_to_int) '21005.csv' Of course, this solution assumes that your file name will consist solely of numerical values.
Fastest way to increment counters in dictionary
What would be the fastest way to increment counters stored in a dictionary? Because I have to do this same operation hundreds of thousand of times, I'm looking for something more efficient than what I have below: def funcA(a): keys = [x for x in range(1, 51)] adict = {key: 0 for key in keys} for k in adict.keys(): # this is the code I would like to improve if k <= a: adict[k] += 1 else: break import timeit number = 100000 t1 = timeit.timeit( 'funcA(5)', setup="from __main__ import funcA", number=number) print(t1) >>> 0.42629639082588255 Trying to use a list comprehension instead seems to slow down everything a bit, maybe because it's lacking the break statement? def funcB(a): # not working, invalid syntax keys = [x for x in range(1, 51)] adict = {key: 0 for key in keys} def _inc(x): x += 1 return x [_inc(adict[k]) for k in adict.keys() if k <= a] # Timing: 0.5831785711925477 Note: initially I had if float(k) <= float(a): but since I'm only expecting numbers (integers or floats), removing the float() conversion improved the code. Is this assumption reasonable? Note2: as noted in several comments, the break statement can give unexpected results in the resulting dictionary, so is better to just do: def funcA(a): keys = [x for x in range(1, 51)] adict = {key: 0 for key in keys} for k in adict: if k <= a: adict[k] += 1 # Timing: 0.5132114209700376
In your case you could just use the fact that booleans (the result of the comparison) can be simply converted to integers. It may not be the fastest but it's definitely short and "relatively" fast: def funcA(a): adict = {key: int(key <= a) for key in range(1, 51)} This is assuming that the second function is actually what you want because the first one could give different results because of the break. Dictionaries are unordered so it could not increment some values for keys smaller or equal to a. Also it doesn't increment the values, it just sets them to 1 or 0 because you actually don't need addition in this case. However, that's not necessarily the fastest way because it has to do a lot of functions calls and int lookups. So I'll present some more equivalent operations in order of performance (fastest to slowest): def cached_version(): range_cache = range(1, 51) cache = dict.fromkeys(range_cache, 0) def inner(a): adict = cache.copy() for key in range_cache[:a]: # requires a to be an integer! adict[key] = 1 return adict return inner func1 = cached_version() # initialize cache def func2(a): keys = range(1, 51) adict = dict.fromkeys(keys[:a], 1) # requires a to be an integer! for key in keys[a:]: adict[key] = 0 return adict def func3(a): adict = {} for key in range(1, 51): if key <= a: adict[key] = 1 else: adict[key] = 0 return adict def func4(a): return {key: 1 if key <= a else 0 for key in range(1, 51)} def func5(a): keys = range(1, 51) adict = dict.fromkeys(keys[:a], 1) # requires a to be an integer! adict.update(dict.fromkeys(keys[a:], 0)) return adict def func6(a): return dict(zip(range(1, 51), [1]*a + [0]*(49-a))) # requires a to be an integer! from itertools import chain def func7(a): return dict(zip(range(1, 51), chain([1]*a, [0]*(49-a)))) # requires a to be an integer! def func8(a): # the one I originally mentioned adict = {key: int(key <= a) for key in range(1, 51)} The timings were done on Python 3.5, Windows 10, there could be differences on other machines and other Python versions. Also note that the performance could be totally different if you had more keys instead of just range(1, 51).
How to compare a list of string and a string, return the best match string from the list?
My problem is on my second function code. This is my code so far.... def simi(d1,d2): dna_1 = d1.lower() dna_2 = d2.lower() lst = [] i = 0 while i < len(dna_1): if dna_1[i] == dna_2[i]: lst.append(1) i += 1 return len(lst) / len(d1) def match(list_1, d , s): dna = [] for item in list_1: dna.append(simi(item, d)) if max(dna) < s: return None return list_1[max(dna)]
You have two problems, the first is you return in the loop before you have tried all the elements, secondly your function simi(item, d)returns a float if it works correctly so trying to index a list with a float will also fail. There is no way your code could do anything but error or return None. I imagine you want to keep track of the best each iteration and return the item that is the best based on what it's simi distance calc is and if the simi is > s or else return None: def match(list_1, d , s): best = None mx = float("-inf") for item in list_1: f = simi(item, d) if f > mx: mx = f best = item return best if mx > s else None You can also use range in simi instead of your while loop with a list comp: def simi(d1,d2): dna_1 = d1.lower() dna_2 = d2.lower() lst = [1 for i in range(len(dna_1)) if dna_1[i] == dna_2[i] ] return len(lst) / len(dna_1) But if you just want to add 1 each time they condition is True you can use sum: def simi(d1,d2): dna_1 = d1.lower() dna_2 = d2.lower() sm = sum(dna_1[i] == dna_2[i] for i in range(len(dna_1))) return sm / len(dna_1)
Using some builtins: from functools import partial similarity_with_sample = partial(simi, 'TACgtAcGaCGT') Now similarity_with_sample is a function that takes one argument, and returns its similarity with 'TACgtAcGaCGT'. Now use that as the key argument of the builtin max function: best_match = max(list_of_samples, key=similarity_with_sample) I'm not sure what your s variable is doing.
Find the most common element in a list
What is an efficient way to find the most common element in a Python list? My list items may not be hashable so can't use a dictionary. Also in case of draws the item with the lowest index should be returned. Example: >>> most_common(['duck', 'duck', 'goose']) 'duck' >>> most_common(['goose', 'duck', 'duck', 'goose']) 'goose'
A simpler one-liner: def most_common(lst): return max(set(lst), key=lst.count)
Borrowing from here, this can be used with Python 2.7: from collections import Counter def Most_Common(lst): data = Counter(lst) return data.most_common(1)[0][0] Works around 4-6 times faster than Alex's solutions, and is 50 times faster than the one-liner proposed by newacct. On CPython 3.6+ (any Python 3.7+) the above will select the first seen element in case of ties. If you're running on older Python, to retrieve the element that occurs first in the list in case of ties you need to do two passes to preserve order: # Only needed pre-3.6! def most_common(lst): data = Counter(lst) return max(lst, key=data.get)
With so many solutions proposed, I'm amazed nobody's proposed what I'd consider an obvious one (for non-hashable but comparable elements) -- [itertools.groupby][1]. itertools offers fast, reusable functionality, and lets you delegate some tricky logic to well-tested standard library components. Consider for example: import itertools import operator def most_common(L): # get an iterable of (item, iterable) pairs SL = sorted((x, i) for i, x in enumerate(L)) # print 'SL:', SL groups = itertools.groupby(SL, key=operator.itemgetter(0)) # auxiliary function to get "quality" for an item def _auxfun(g): item, iterable = g count = 0 min_index = len(L) for _, where in iterable: count += 1 min_index = min(min_index, where) # print 'item %r, count %r, minind %r' % (item, count, min_index) return count, -min_index # pick the highest-count/earliest item return max(groups, key=_auxfun)[0] This could be written more concisely, of course, but I'm aiming for maximal clarity. The two print statements can be uncommented to better see the machinery in action; for example, with prints uncommented: print most_common(['goose', 'duck', 'duck', 'goose']) emits: SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)] item 'duck', count 2, minind 1 item 'goose', count 2, minind 0 goose As you see, SL is a list of pairs, each pair an item followed by the item's index in the original list (to implement the key condition that, if the "most common" items with the same highest count are > 1, the result must be the earliest-occurring one). groupby groups by the item only (via operator.itemgetter). The auxiliary function, called once per grouping during the max computation, receives and internally unpacks a group - a tuple with two items (item, iterable) where the iterable's items are also two-item tuples, (item, original index) [[the items of SL]]. Then the auxiliary function uses a loop to determine both the count of entries in the group's iterable, and the minimum original index; it returns those as combined "quality key", with the min index sign-changed so the max operation will consider "better" those items that occurred earlier in the original list. This code could be much simpler if it worried a little less about big-O issues in time and space, e.g....: def most_common(L): groups = itertools.groupby(sorted(L)) def _auxfun((item, iterable)): return len(list(iterable)), -L.index(item) return max(groups, key=_auxfun)[0] same basic idea, just expressed more simply and compactly... but, alas, an extra O(N) auxiliary space (to embody the groups' iterables to lists) and O(N squared) time (to get the L.index of every item). While premature optimization is the root of all evil in programming, deliberately picking an O(N squared) approach when an O(N log N) one is available just goes too much against the grain of scalability!-) Finally, for those who prefer "oneliners" to clarity and performance, a bonus 1-liner version with suitably mangled names:-). from itertools import groupby as g def most_common_oneliner(L): return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
What you want is known in statistics as mode, and Python of course has a built-in function to do exactly that for you: >>> from statistics import mode >>> mode([1, 2, 2, 3, 3, 3, 3, 3, 4, 5, 6, 6, 6]) 3 Note that if there is no "most common element" such as cases where the top two are tied, this will raise StatisticsError on Python <=3.7, and on 3.8 onwards it will return the first one encountered.
Without the requirement about the lowest index, you can use collections.Counter for this: from collections import Counter a = [1936, 2401, 2916, 4761, 9216, 9216, 9604, 9801] c = Counter(a) print(c.most_common(1)) # the one most common element... 2 would mean the 2 most common [(9216, 2)] # a set containing the element, and it's count in 'a'
If they are not hashable, you can sort them and do a single loop over the result counting the items (identical items will be next to each other). But it might be faster to make them hashable and use a dict. def most_common(lst): cur_length = 0 max_length = 0 cur_i = 0 max_i = 0 cur_item = None max_item = None for i, item in sorted(enumerate(lst), key=lambda x: x[1]): if cur_item is None or cur_item != item: if cur_length > max_length or (cur_length == max_length and cur_i < max_i): max_length = cur_length max_i = cur_i max_item = cur_item cur_length = 1 cur_i = i cur_item = item else: cur_length += 1 if cur_length > max_length or (cur_length == max_length and cur_i < max_i): return cur_item return max_item
This is an O(n) solution. mydict = {} cnt, itm = 0, '' for item in reversed(lst): mydict[item] = mydict.get(item, 0) + 1 if mydict[item] >= cnt : cnt, itm = mydict[item], item print itm (reversed is used to make sure that it returns the lowest index item)
Sort a copy of the list and find the longest run. You can decorate the list before sorting it with the index of each element, and then choose the run that starts with the lowest index in the case of a tie.
A one-liner: def most_common (lst): return max(((item, lst.count(item)) for item in set(lst)), key=lambda a: a[1])[0]
I am doing this using scipy stat module and lambda: import scipy.stats lst = [1,2,3,4,5,6,7,5] most_freq_val = lambda x: scipy.stats.mode(x)[0][0] print(most_freq_val(lst)) Result: most_freq_val = 5
# use Decorate, Sort, Undecorate to solve the problem def most_common(iterable): # Make a list with tuples: (item, index) # The index will be used later to break ties for most common item. lst = [(x, i) for i, x in enumerate(iterable)] lst.sort() # lst_final will also be a list of tuples: (count, index, item) # Sorting on this list will find us the most common item, and the index # will break ties so the one listed first wins. Count is negative so # largest count will have lowest value and sort first. lst_final = [] # Get an iterator for our new list... itr = iter(lst) # ...and pop the first tuple off. Setup current state vars for loop. count = 1 tup = next(itr) x_cur, i_cur = tup # Loop over sorted list of tuples, counting occurrences of item. for tup in itr: # Same item again? if x_cur == tup[0]: # Yes, same item; increment count count += 1 else: # No, new item, so write previous current item to lst_final... t = (-count, i_cur, x_cur) lst_final.append(t) # ...and reset current state vars for loop. x_cur, i_cur = tup count = 1 # Write final item after loop ends t = (-count, i_cur, x_cur) lst_final.append(t) lst_final.sort() answer = lst_final[0][2] return answer print most_common(['x', 'e', 'a', 'e', 'a', 'e', 'e']) # prints 'e' print most_common(['goose', 'duck', 'duck', 'goose']) # prints 'goose'
Building on Luiz's answer, but satisfying the "in case of draws the item with the lowest index should be returned" condition: from statistics import mode, StatisticsError def most_common(l): try: return mode(l) except StatisticsError as e: # will only return the first element if no unique mode found if 'no unique mode' in e.args[0]: return l[0] # this is for "StatisticsError: no mode for empty data" # after calling mode([]) raise Example: >>> most_common(['a', 'b', 'b']) 'b' >>> most_common([1, 2]) 1 >>> most_common([]) StatisticsError: no mode for empty data
Simple one line solution moc= max([(lst.count(chr),chr) for chr in set(lst)]) It will return most frequent element with its frequency.
You probably don't need this anymore, but this is what I did for a similar problem. (It looks longer than it is because of the comments.) itemList = ['hi', 'hi', 'hello', 'bye'] counter = {} maxItemCount = 0 for item in itemList: try: # Referencing this will cause a KeyError exception # if it doesn't already exist counter[item] # ... meaning if we get this far it didn't happen so # we'll increment counter[item] += 1 except KeyError: # If we got a KeyError we need to create the # dictionary key counter[item] = 1 # Keep overwriting maxItemCount with the latest number, # if it's higher than the existing itemCount if counter[item] > maxItemCount: maxItemCount = counter[item] mostPopularItem = item print mostPopularItem
ans = [1, 1, 0, 0, 1, 1] all_ans = {ans.count(ans[i]): ans[i] for i in range(len(ans))} print(all_ans) all_ans={4: 1, 2: 0} max_key = max(all_ans.keys()) 4 print(all_ans[max_key]) 1
#This will return the list sorted by frequency: def orderByFrequency(list): listUniqueValues = np.unique(list) listQty = [] listOrderedByFrequency = [] for i in range(len(listUniqueValues)): listQty.append(list.count(listUniqueValues[i])) for i in range(len(listQty)): index_bigger = np.argmax(listQty) for j in range(listQty[index_bigger]): listOrderedByFrequency.append(listUniqueValues[index_bigger]) listQty[index_bigger] = -1 return listOrderedByFrequency #And this will return a list with the most frequent values in a list: def getMostFrequentValues(list): if (len(list) <= 1): return list list_most_frequent = [] list_ordered_by_frequency = orderByFrequency(list) list_most_frequent.append(list_ordered_by_frequency[0]) frequency = list_ordered_by_frequency.count(list_ordered_by_frequency[0]) index = 0 while(index < len(list_ordered_by_frequency)): index = index + frequency if(index < len(list_ordered_by_frequency)): testValue = list_ordered_by_frequency[index] testValueFrequency = list_ordered_by_frequency.count(testValue) if (testValueFrequency == frequency): list_most_frequent.append(testValue) else: break return list_most_frequent #tests: print(getMostFrequentValues([])) print(getMostFrequentValues([1])) print(getMostFrequentValues([1,1])) print(getMostFrequentValues([2,1])) print(getMostFrequentValues([2,2,1])) print(getMostFrequentValues([1,2,1,2])) print(getMostFrequentValues([1,2,1,2,2])) print(getMostFrequentValues([3,2,3,5,6,3,2,2])) print(getMostFrequentValues([1,2,2,60,50,3,3,50,3,4,50,4,4,60,60])) Results: [] [1] [1] [1, 2] [2] [1, 2] [2] [2, 3] [3, 4, 50, 60]
Here: def most_common(l): max = 0 maxitem = None for x in set(l): count = l.count(x) if count > max: max = count maxitem = x return maxitem I have a vague feeling there is a method somewhere in the standard library that will give you the count of each element, but I can't find it.
This is the obvious slow solution (O(n^2)) if neither sorting nor hashing is feasible, but equality comparison (==) is available: def most_common(items): if not items: raise ValueError fitems = [] best_idx = 0 for item in items: item_missing = True i = 0 for fitem in fitems: if fitem[0] == item: fitem[1] += 1 d = fitem[1] - fitems[best_idx][1] if d > 0 or (d == 0 and fitems[best_idx][2] > fitem[2]): best_idx = i item_missing = False break i += 1 if item_missing: fitems.append([item, 1, i]) return items[best_idx] But making your items hashable or sortable (as recommended by other answers) would almost always make finding the most common element faster if the length of your list (n) is large. O(n) on average with hashing, and O(n*log(n)) at worst for sorting.
>>> li = ['goose', 'duck', 'duck'] >>> def foo(li): st = set(li) mx = -1 for each in st: temp = li.count(each): if mx < temp: mx = temp h = each return h >>> foo(li) 'duck'
I needed to do this in a recent program. I'll admit it, I couldn't understand Alex's answer, so this is what I ended up with. def mostPopular(l): mpEl=None mpIndex=0 mpCount=0 curEl=None curCount=0 for i, el in sorted(enumerate(l), key=lambda x: (x[1], x[0]), reverse=True): curCount=curCount+1 if el==curEl else 1 curEl=el if curCount>mpCount \ or (curCount==mpCount and i<mpIndex): mpEl=curEl mpIndex=i mpCount=curCount return mpEl, mpCount, mpIndex I timed it against Alex's solution and it's about 10-15% faster for short lists, but once you go over 100 elements or more (tested up to 200000) it's about 20% slower.
def most_frequent(List): counter = 0 num = List[0] for i in List: curr_frequency = List.count(i) if(curr_frequency> counter): counter = curr_frequency num = i return num List = [2, 1, 2, 2, 1, 3] print(most_frequent(List))
Hi this is a very simple solution, with linear time complexity L = ['goose', 'duck', 'duck'] def most_common(L): current_winner = 0 max_repeated = None for i in L: amount_times = L.count(i) if amount_times > current_winner: current_winner = amount_times max_repeated = i return max_repeated print(most_common(L)) "duck" Where number, is the element in the list that repeats most of the time
numbers = [1, 3, 7, 4, 3, 0, 3, 6, 3] max_repeat_num = max(numbers, key=numbers.count) *# which number most* frequently max_repeat = numbers.count(max_repeat_num) *#how many times* print(f" the number {max_repeat_num} is repeated{max_repeat} times")
def mostCommonElement(list): count = {} // dict holder max = 0 // keep track of the count by key result = None // holder when count is greater than max for i in list: if i not in count: count[i] = 1 else: count[i] += 1 if count[i] > max: max = count[i] result = i return result mostCommonElement(["a","b","a","c"]) -> "a"
The most common element should be the one which is appearing more than N/2 times in the array where N being the len(array). The below technique will do it in O(n) time complexity, with just consuming O(1) auxiliary space. from collections import Counter def majorityElement(arr): majority_elem = Counter(arr) size = len(arr) for key, val in majority_elem.items(): if val > size/2: return key return -1
def most_common(lst): if max([lst.count(i)for i in lst]) == 1: return False else: return max(set(lst), key=lst.count)
def popular(L): C={} for a in L: C[a]=L.count(a) for b in C.keys(): if C[b]==max(C.values()): return b L=[2,3,5,3,6,3,6,3,6,3,7,467,4,7,4] print popular(L)