What I want to do is two things actually, first, i need to make the following list:
teammates={"Ali":[1,5,8],"James":[3,2,1],"Fred":[9,4,2]}
and have the lists of numbers added up and divided by 3 (working out the average) and be sorted from highest to lowest like like this:
Fred : 5
Ali : 4.7
James : 2
Secondly, i need to make the program only print the highest score for each team mate and then sorted from highest to lowest like this:
Fred : 9
Ali : 8
James : 3
This would be really helpful as im using it for my sons birthday party I have searched through almost every single question similar to this and couldnt find anything at all that worked... Any help would be greatly appreciated. Thanks :)
using sorted
and lambda
>>> teammates={"Ali":[1,5,8],"James":[3,2,1],"Fred":[9,4,2]}
>>> sorted([[x,sum(y)/float(len(y))] for x,y in teammates.items()],key=lambda x:x[1])
[['James', 2.0], ['Ali', 4.666666666666667], ['Fred', 5.0]]
on high score:
>>> sorted([[x,max(y)] for x,y in teammates.items()],key=lambda x:x[1],reverse=True)
[['Fred', 9], ['Ali', 8], ['James', 3]]
I recommend you to use max()
to get the max value on a list, sum()
to get the sum of all elements of a list, len()
to get the size of an array:
teammates={"Ali":[1,5,8],"James":[3,2,1],"Fred":[9,4,2]}
# Use sum and len to get average
teammates_average={
"Ali":sum(teammates['Ali'])/float(len(teammates['Ali'])),
"James":sum(teammates['James'])/float(len(teammates['James'])),
"Fred":sum(teammates['Fred'])/float(len(teammates['Fred'])),
}
Output: {'Ali': 4.666666666666667, 'Fred': 5.0, 'James': 2.0}
# Use max to get the highest value
teammates_highest={
"Ali":max(teammates['Ali']),
"James":max(teammates['James']),
"Fred":max(teammates['Fred']),
}
Output: {'Ali': 8, 'Fred': 9, 'James': 3}
# Order last output
sorted(teammates_highest.items(), key=operator.itemgetter(1), reverse=True)
Output: [('Fred', 9), ('Ali', 8), ('James', 3)]
sort a list inside dictionary is
>>>>sorted(teammates.items(), key=lambda (k, v): v[1], reverse=True)
>>>>[('Ali', [1, 5, 8]), ('Fred', [9, 4, 2]), ('James', [3, 2, 1])]
We'll use 3 different functions, whose name are sufficiently descriptive...
def dict_of_means(d):
return {k:sum(v)*1.0/v.count() for k, v in d.items()}
note the multiplication by 1.0 to force a float.
def dict_of_max(d):
return {k:max(v) for k, v in d.items()}
and finally
def print_sorted(d,is_float=0):
l = [(v, k) for k, v in d.items()]
l.sort()
for v, k in l[::-1]:
if is_float:
print "%-12s%5.2" % (k+" :", v)
else:
print "%-12s%2d" % (k+" :", v)
An example of usage
In [31]: teammates={"Ali":[1,5,8],"James":[3,2,1],"Fred":[9,4,2]}
In [32]: print_sorted(dict_of_max(teammates))
Fred : 9
Ali : 8
James : 3
In [33]: print_sorted(dict_of_means(teammates), 1)
Fred : 5.00
Ali : 4.67
James : 2.00
In [34]:
COMMENTS
There is a recurring idiom for k, v in d.items()
that is used in all the three functions. The dictionary method d.items()
gives you a list of tuples, eg,
In [34]: teammates.items()
Out[34]: [('James', [3, 2, 1]), ('Fred', [9, 4, 2]), ('Ali', [1, 5, 8])]
and you assign the content of each tuple by unpacking in turn each of them into the loop variables k
(for key, it's quite conventional) and v
(for value).
At this point, in what is called a comprehension , you build either a dictionary using curly brackets '{}' or a list using straight brackets'[]' that you can return to the caller, or that you can further process as it is the case in print_sorted
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.