[Tutor] Sorting/filtering data, dictionary question & Re:OT

Bill Burns billburns at pennswoods.net
Tue Jan 4 02:34:39 CET 2005


[Kent]
> This is actually a common approach to sorting a list in Python - add enough
> fields to the list so it sorts the way you want, then filter out the fields
> you don't need any more. It even has a name, it's called Decorate - Sort -
> Undecorate. In your case the 'decorate' step is built-in to the dictionary,
> you are just doing the sort and undecorate.

[Bill]
Thank you, I'm glad I was actually doing something correctly. I didn't realize
it was Decorate - Sort - Undecorate, now I know!

[Kent]
> There is a shorter way to write filterPipeData() using a list
> comprehension, which is just a shortcut for what you wrote:
>
> def filterPipeData(data):
>      return [ (typ,inchSize,weight) for typ, size, inchSize, weight in data
> ]
>
> When you get used to it, this form is more readable; it will also be faster
> though I don't think you will notice.

[Bill]
I've seen list comprehensions, but I didn't understand how they worked.
Since I understand how my function works, it's not so hard to figure out
what your doing with this list comp. I guess the brackets ([]) signify a list.
Then the for loop is placed on the right-hand side and then on the left, you
append to "the list". I think the other list comprehensions I saw before
were performing more operations, but this one is not that hard to figure out.

[Kent]
> If you calculate gallons per foot from your current ID values and put those
> numbers in the dict, the new numbers will not be any more accurate than
> your current calculations. If you have another source for gal/foot that you
> think is more accurate then you could use those numbers. But I think what
> you have is probably good enough and if it is working there is no need to
> change it.

[Bill]
This sounds good to me. I'll keep the dict/function as is.

Again, Thank you! I appreciate your feedback!!

Bill


More information about the Tutor mailing list