How to find the most common element in the list, and if there is a connection, then who is the last last?

Basically, if a list is given

events = [123,123,456,456,456,123] 

I expect him to return 456 because 456 was last seen before 123 was last seen.

I made lists consisting of samples and indices of the original list of numbers. I also made a dictionary in which the key is an element from events (the original part), and the hte value is the .count() key.

I really don't know where to go from here and can use some help.

+2
python
source share
3 answers

An approach

Find the most common items (Counter.most_common). Then find the item among those candidates who have a minimum index (list in the index dictionary, min {index: key} .iteritems ()).

The code

Stealing liberally from @gnibbler and @Jeff:

 from collections import Counter def most_frequent_first(events): frequencies = Counter(events) indexes = {event: i for i, event in enumerate(events)} most_frequent_with_indexes = {indexes[key]: key for key, _ in frequencies.most_common()} return min(most_frequent_with_indexes.iteritems())[1] events = [123,123,456,456,456,123, 1, 2, 3, 2, 3] print(most_frequent_first(events)) 

Result

 >>> print(most_frequent_first(events)) 456 

The code

The best part of the code will provide you with frequency and index, showing that the code is working correctly. Here is an implementation that uses named_tuple:

 from collections import Counter, namedtuple frequent_first = namedtuple("frequent_first", ["frequent", "first"]) def most_frequent_first(events): frequencies = Counter(events) indexes = {event: i for i, event in enumerate(events)} combined = {key: frequent_first(value, indexes[key]) for key, value in frequencies.iteritems()} return min(combined.iteritems(), key=lambda t: (-t[1].frequent, t[1].first)) events = [123,123,456,456,456,123, 1, 2, 3, 2, 3] print(most_frequent_first(events)) 

Result

 >>> print(most_frequent_first(events)) (456, frequent_first(frequent=3, first=4)) 
+4
source share

Use collections.counter

 >>> import collections >>> events = [123,123,456,456,456,123] >>> counts = collections.Counter(events) >>> print counts Counter({456: 3, 123: 3}) >>> mostCommon = counts.most_common() >>> print mostCommon [(456, 3), (123, 3)] 

This is the hard part.

+2
source share
 >>> from collections import Counter >>> events = [123,123,456,456,456,123] >>> c = Counter(events) >>> idxs = {k: v for v,k in enumerate(events)} >>> sorted(c.items(), key=lambda (k,v): (-v, idxs[k])) [(456, 3), (123, 3)] 
+2
source share

All Articles