how can I sort a bunch of lists over multiple fields?
googleboy
mynews44 at yahoo.com
Thu Apr 28 13:54:33 EDT 2005
I'd be just such a newbie; I don't understand why it would matter if I
left the book instance referencing itself....
However these wonderful responses have gotten me a very long way
towards my goal. I just have a couple of quick questions.
firstly, I am trying hard to figure out how to create a new file with
the list rather than print to standard out. I haev done this:
for book in books:
print book # just to be sure it works as I expect
sort1 = open(r'D:\path to\sort1.csv', 'w+')
print >> sort1, book
sort1.close()
and this creates the file as I expect, however it creates it populated
with only the information of the final book in the sorted list. I am
guessing I need to figure out how to append as part of this loop, but
the only info I have found so far suggests this should append by
default....?
Secondly, I am wondering how I can get a search algorithm that will
search by multiple fields here, so that I can (as one example) sort
the books out by author and then date, to present a list of the book
grouped by authors and having each group presented in a chronological
order, or by author and title, grouping all the books up into authors
presenting each group alphabetically by title. Or by publisher and
date, or by publisher and code....
I have tried things like
books.sort(key = operator.attrgetter("author"), key =
operator.attrgetter("title") and
books.sort(key = operator.attrgetter("author", "title")
but they both give errors.
Is this where using cmd functions instead of keys becomes necessary?
Thanks!
googleboy
More information about the Python-list
mailing list