how can I sort a bunch of lists over multiple fields?
Steven Bethard
steven.bethard at gmail.com
Thu Apr 28 14:43:19 EDT 2005
googleboy wrote:
> 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.
You're reopening the file on each iteration of the loop. I think you
want to open it only once, before the loop, e.g.
sort1_file = open(r'D:\path to\sort1.csv', 'w+')
for book in books:
sort1_file.write('%s\n' % book) # same as "print >> sort1, book"
sort1_file.close()
Note that the opening and closing of the file is outside the loop.
> 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.
The problem is that operator.attrgetter only accepts a single attribute.
Basically, attrgetter looks something like:
def attrgetter(attr_name):
def func(obj):
return getattr(obj, attr_name)
return func
So attrgetter can't really solve your problem. However, you can create
a similar function that should do the job. Something like (untested):
def get_key(*attr_names):
def key(book):
return [getattr(book, name) for name in attr_names)]
return key
Then you should be able to do something like:
books.sort(key=get_key("author", "title"))
The trick is that the inner function, 'key', looks up a sequence of
attributes on the book object, instead of just a single attribute like
attrgetter does.
HTH,
STeVe
More information about the Python-list
mailing list