-1

I'm creating a script in python that gets the user's name and lap time, it then saves it into a dictionary which then saves it to a file. How do I sort the dictionary so it shows the lowest numbers first and then the four more scores after that?

def lap():

    username = str(input("Name: "))
    time = str(input("Lap Time: "))
    lap_det = {}

    lap_det = str([username + " : " + time])

    with open("lap_time.txt", "w") as f:
        f.write(lap_det)

    with open("lap_time.txt", "r") as f:
        print(f.readlines())

    user_r = input("Press r to restart >>> ")
    if user_r == "r":
        lap()
    else:
        quit()

lap()
Taku
  • 31,927
  • 11
  • 74
  • 85
  • 2
    1. Post code, correctly formatted, **as text**. 2. SO isn't a tutorial service; if your code needs additional functionality, make an effort to implement it. – jonrsharpe May 17 '17 at 20:01
  • Dictionaries are unordered, and cannot be sorted. – juanpa.arrivillaga May 17 '17 at 20:01
  • 1
    Just saying, you don't even have a dict in your code, the moment you defined the dict, you override it with a str. Ie. lap_det – Taku May 17 '17 at 20:05
  • You can't change the structure of a dictionary. Are you asking how to display the contents of a dictionary in a particular order? – Peter Wood May 17 '17 at 20:05
  • `input` returns `str`, you don't need to convert the values. – Peter Wood May 17 '17 at 20:06
  • 1
    Possible duplicate of [Sort a Python dictionary by value](http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value) – bouletta May 17 '17 at 20:59

2 Answers2

0

Python's dict is unordered by default. However in Python 3.6, the dictionary currently keeps its insertion order. Anyway, I would recommend using an OrderedDict:

>>> laps = {'foo': 12, 'bar': 10, 'fizz': 15, 'buzz': 7}
>>> OrderedDict(sorted(laps.items(), key=lambda k: k[1]))
OrderedDict([('buzz', 7), ('bar', 10), ('foo', 12), ('fizz', 15)])

There are some other SO answers on this subject, such as this one:

Community
  • 1
  • 1
Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
-1

You can't sort a dictionary.

Though, if you want to get the values of the dictionary in the same order as the keys, you can use:

for key in sorted(dic):
    print(dic[key])

(Note: this will just print them)

Please note that using sorted(dic) will not sort the dictionary, but only iterate over its keys (since iterating over a dict dic iterates over dic.keys())

Thomas Kowalski
  • 1,995
  • 4
  • 20
  • 42
  • 1
    Don't use `sorted(dic.keys())` In Python 2 this is inefficient, since it creates a *new list* of keys each time. In Python 3, it is merely redundant, since it returns a view of the keys. In any event, all you need is `sorted(dic)` to get a list of sorted keys. – juanpa.arrivillaga May 17 '17 at 20:03
  • I was assuming he was using Python 3 since as a beginner he has no reason not to. Also, using `sorted(dic)` may have been ambiguous, he could have though that you could sort the dictionary (since I don't think he knows iterating on a `dict` iterates on its keys...) – Thomas Kowalski May 17 '17 at 20:05
  • 1
    Right, *even if you use Python 3, you should not use `dic.keys()` to iterate over keys...*. It is idiomatic to just use `dic` for that purpose. At the very least, it is adding a needless method call. – juanpa.arrivillaga May 17 '17 at 20:06
  • 1
    Right, and I don't think it is a valid reason. At the very least, it goes against Python idioms. – juanpa.arrivillaga May 17 '17 at 20:07