-2

Can someone explain to me what this line of code means, keep in mind im new to python.

I have to print the name of the best student and his grade. d is a composed of names of students and their respective means(grades mean)

I know this code works but i can't understand it.

b_p = d.keys()[d.values().index(max(d.values()))]

print '%s %.2f' % (b_p,d[b_p])

I tried sorting d and print d.keys()[-1] but

AttributeError: 'list' object has no attribute 'keys'

. I also have to print his grade next to the name.

Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51
Catalin
  • 81
  • 7
  • *"the name of the best student and his grade"* – `"name" --> "key"`, `"grade" --> "value"`. – meowgoesthedog May 15 '19 at 09:27
  • 3
    It means whoever authored that line of code has never learned how to write proper python code. That's *horrible*. – Aran-Fey May 15 '19 at 09:28
  • Please add how `d` looks like in the question @Catalin – Devesh Kumar Singh May 15 '19 at 09:30
  • inline `d={} for i in range(m): name=raw_input() grade=raw_input() grade=map(int,note.split(" ")) mean=float(sum(grade))/len(grade) if mean >= 8.00: if not any ([int(x) < 5 for x in grade]): d[name]=mean ` i know keys() brings the name and values() the grades. But i cant seem to print them together. Also i have to set precision of 2 decimals to the grade – Catalin May 15 '19 at 09:35

2 Answers2

1

Decipher it step by step, from the inside outwards:

d.values()

gets you a list (in Python 2) of values of your dictionary. That is, the grades themselves. The highest value is then obtained:

max(d.values())

Then, the index (position) is found in the list of values for that highest value:

d.values().index(max(d.values()))

That index applies to the list of values, but also to the list of keys. So you can use that index to find the key (student name) related to the highest value, and index that list (d.keys()) with it:

d.keys()[d.values().index(max(d.values()))]

And thus, the result is the name of the person with the highest grade.

A clearer variant is to split it up into multiple lines:

grades = d.values()
maxgrade = max(grades)
maxindex = grades.index(grades)
names = d.keys()
maxname = names[maxindex]

Why your attempt failed:

I tried sorting d and print d.keys()[-1] but [got an error message:]

AttributeError: 'list' object has no attribute 'keys'

When you sort a dictionary, the result is a sorted list of just the keys: you lose the dictionary information. So after d = sorted(d), d is now a list, and does not have a method, or more generally, attribute, keys(). Hence the error message.


As to how to do this properly, refer to this question. In particular, the clearest answer there is

max(d, key=d.get)

The max function can take a key argument, and using d.get will actually use whatever d.get returns (which are the values, not the keys) to find the maximum.
You could use the same with sorted:

sorted(d, key=d.get)

will list the names, ordered from lowest to highest grade.

9769953
  • 10,344
  • 3
  • 26
  • 37
  • It works when i split them in lines thank u, but now the output is a string : `('Ilie David', '10.00')` and how it should have looked `Ilie David 10.00` – Catalin May 15 '19 at 10:11
  • 1
    @Catalin that is a different issue, and just has to do with the representation. It definitely does not happen with your listed code, so your doing something else there. Please ask a new question about this specifically, or try playing around with things a bit more. – 9769953 May 15 '19 at 10:51
0

Notice that sorted(d) is a shorthand for sorted(d.keys()), not sorted(d.values()) or sorted(d.items())

To sort the key-value pairs by value, try:

from operator import itemgetter
best_student = sorted(d.items(), key=itemgetter(1), reverse=True)[0]
Arnie97
  • 1,020
  • 7
  • 19