[Tutor] Sorting numbers
Jeff Shannon
jeff@ccvcorp.com
Wed Feb 5 12:37:01 2003
Adam Vardy wrote:
>Tuesday, February 4, 2003, 4:27:01 PM, you wrote:
>
>>> >>> rawdata
>>>['45K', '100K', '3.4Meg', '17K', '300K', '9.3Meg', '512', '23Meg']
>>> >>> srt = {}
>>> >>> for item in rawdata:
>>>... num, suffix = splitsuffix(item)
>>>... value = srt.get(suffix, [])
>>>... value.append(num)
>>>... srt[suffix] = value
>>>
>>>
>
>Can you explain the last three lines here?
>
>
Sure. First let's look at the line 'value = srt.get(suffix, [])'. We
have a dictionary called srt, and we want to see if it has anything in
the key that's held in suffix. I could've said 'value = srt[suffix]',
and then value would contain whatever had previously been stored in the
dictionary with that key, but what happens if nothing had been
previously stored?
>>> srt = {}
>>> value = srt['Meg']
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
KeyError: Meg
>>>
We get an exception. Now, since I'm starting with an empty dictionary,
I know that I'm going to try looking up nonexistent keys at least a few
times. I *could* use a try/except block, and respond to the exception,
but there's an easier way. Dictionaries have a convenient get() method,
which will look up a key like normal, but if that key is not found then
it will return a default value.
>>> value = srt.get('Meg', [])
>>> value
[]
>>> value = srt.get('Meg', [])
>>> value
['2.4', '9.6']
>>>
This basically asks the dictionary, "Give me whatever you've got for
'Meg', but if you don't have anything, then give me an empty list instead."
Now 'value' is a list containing whatever had been previously been
stored in the dictionary with that suffix -- or an empty list if nothing
had been stored previously. The next line, 'value.append(num)', tacks
the current numeric value onto the end of that list. Finally, the last
line 'srt[suffix] = value' takes the modified list and stores that list
back in the dictionary, under the same key as before.
The overall effect of these three lines, then, is that the list that the
dictionary has stored under a given suffix has the current value of num
tacked on the end, with new lists being created as necessary when new
suffixes are found. As the original list of raw data is iterated
through, it's sorted into a series of lists depending on what the
non-numeric suffix on the string is. Eventually, all of the items that
had ended in 'Meg' are stored in srt['Meg'], all of the numbers that had
ended in 'K' are stored in srt['K'], and so on.
Hope that helps. If there's anything else you're not clear on, feel
free to ask.
Jeff Shannon
Technician/Programmer
Credit International