Page 184 -
P. 184

time trials


          The trouble with time


           Well…there’s never enough of it, is there?
           Let’s look closely at the coach’s data to see what the problem is. Here’s Sarah
           raw data again:                                                        Oh, look: what a lovely
                                                                                  bunch of strings...



            2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55
                                                         sarah.txt


           Recall that data read from a file comes into your program as text, so Sarah’s
           data looks like this once you turn it into a list of “times”:



            ['2:58', '2.58', '2:39’, '2-25', '2-55', '2:54’, '2.18', '2:55', '2:55']


                                           These are all strings, even though the coach
                                           thinks they’re times.




          And when you sort Sarah’s data, it ends up in this order (which isn’t quite
           what you were expecting):                                                    I don’t get what the
                                                                                        problem is...they’re all
                                                                                        times to me.

             ['2-25', '2-55', '2.18', '2.58', '2:39', '2:54', '2:55', '2:55', '2:58']

                                                Whoops again. 2:39
                   Whoops! That’s not
                   right. How can 2.18          can’t come between
                   come after 2-55?             2.58 and 2:54, can it?




           Python sorts the strings, and when it comes to strings, a dash comes before a
           period, which itself comes before a colon. As all the strings start with 2, the
           next character in each string acts like a grouping mechanism, with the dashed
           times grouped and sorted, then the period times, and finally the colon times.

           Nonuniformity in the coach’s data is causing the sort to fail.


           148    Chapter 5
   179   180   181   182   183   184   185   186   187   188   189