Reputation: 19
Lets say the days of the week are in a ordered list:
days_week=['mon','tues','wed','thurs','fri','sat']
The function I'm making recieves a list of randomly occuring elements in days_week
:
random_list=['mon','mon','mon','wed','sat','fri','fri','wed']
And then it should output a tuple of the highest occuring day and a list of the occurences of each day in the the right order as in days_week
e.g. all mon's first, then all tue's:
output:('mon',[3,0,2,0,2,1])
My first thought was to build a dictionary of keys that are the names of the week, and values that are the occurences of those days:
days_dictionary={}
for i in random_list:
if i in days_dictionary:
days_dictionary[i]+=1
else:
days_dictionary[i]=1
and that's where I'm stuck because I'm not sure how I would use the dictionary to form the output above.
edit: i cant import anything other than math
Upvotes: 0
Views: 95
Reputation: 12983
The first step is to get the most common day, like so:
import operator
most_common = max(days_dictionary.iteritems(), key=operator.itemgetter(1))[0]
then make the list of the rest of the occurrences
occur = [days_dictionary[day] for day in days_week]
then make the tuple
(most_common, occur)
Upvotes: 0
Reputation: 304225
you should be able to use these two expressions with your days_dictionary
>>> max(days_dictionary, key=days_dictionary.get)
'mon'
>>> [days_dictionary.get(k, 0) for k in days_week]
[3, 0, 2, 0, 2, 1]
Another way is to use a collections.Counter
>>> import random
>>> from collections import Counter
>>> days_week = ['mon', 'tues', 'wed', 'thurs', 'fri', 'sat']
>>> random_list = [random.choice(days_week) for x in range(10)]
>>> random_list
['wed', 'mon', 'mon', 'tues', 'tues', 'mon', 'wed', 'mon', 'wed', 'sat']
>>> c = Counter(random_list)
>>> c.most_common(1)[0][0]
'mon'
>>> [c.get(k, 0) for k in days_week]
[4, 2, 3, 0, 0, 1]
>>> c.most_common(1)[0][0], [c.get(k, 0) for k in days_week]
('mon', [4, 2, 3, 0, 0, 1])
Upvotes: 3
Reputation: 8925
I suggest having a look at itertools.groupby:
>>> days_week=['mon','tues','wed','thurs','fri','sat']
>>> import random
>>> random_list = [random.choice(days_week) for _ in range(10)]
>>> print random_list
['mon', 'fri', 'sat', 'wed', 'sat', 'thurs', 'wed', 'sat', 'tues', 'tues']
>>> import itertools
>>> g = itertools.groupby(sorted(enumerate(random_list), key=lambda x: x[1]), lambda x: x[1])
>>> for day, occur in g:
print day, list(occur)
fri [(1, 'fri')]
mon [(0, 'mon')]
sat [(2, 'sat'), (4, 'sat'), (7, 'sat')]
thurs [(5, 'thurs')]
tues [(8, 'tues'), (9, 'tues')]
wed [(3, 'wed'), (6, 'wed')]
Upvotes: 2
Reputation: 47790
First: you can use collections.Counter to build your dictionary:
from collections import Counter
random_list = ['mon','mon','mon','wed','sat','fri','fri','wed']
counts = Counter(random_list)
Then you can build the frequency list like so:
days_week = ['mon','tues','wed','thurs','fri','sat']
freqs = [counts[d] for d in days_week if d in counts]
And for the final output:
output = counts.most_common(1)[0][0], freqs ## ('mon', [3, 2, 2, 1])
Upvotes: 1