All | 43 Folders | Accessibility | BoingBoing | Books | Computer Related | Family | Films | General | Hacking | Hobbies | Humor | Java | Links | Omni | OpenSolaris | Puzzles and Games

« Previous day (Sep 15, 2007) | Main | Next day (Sep 17, 2007) »
20070916 Sunday September 16, 2007

1001 Books You Must Read Before You Die - Part 2

I just couldn't leave it alone. I had to keep scratching at it, like a festering wound.

I knew from the Amazon reviewers that Ian McEwan had eight books on the list. I was curious to know who the other multi-book authors were. First of all, I grabbed the list, cleaned it up and saved it as a text file.

I then wrote a small Python script to read in that text file and determine two things:

Here's the code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Change to False to print a book list alphabetically by author.
printSummary = True

authors = {}
bookFile = open("1001Books.txt", "r")
while 1:
    line = bookFile.readline()
    if not line:
        break
    tokens = line.split("–")
    authorString = tokens[1].lstrip().rstrip()
    title =  tokens[0].split(".", 1)[1].lstrip().rstrip()

    # Adjust author string so last name comes first.
    tokens = authorString.split()
    author = tokens[len(tokens)-1]
    if len(tokens)-1 > 0:
        author = author + ", " + " ".join(tokens[0:len(tokens)-1])

    if authors.has_key(author):
        authors[author].append(title)
    else:
        authors[author] = [ title ]

if printSummary:
    # ------ Print summary of multi-book authors ------
    noOfBooks = {}
    for author in authors.keys():
        total = len(authors[author])
        if total > 1:
            if noOfBooks.has_key(total):
                noOfBooks[total].append(author)
            else:
                noOfBooks[total] = [ author ]

    print "Number of authors: ", len(authors), "\n\n"

    keys = noOfBooks.keys()
    keys.sort()
    for key in keys:
        print "Authors with ", key, " books: "
        noOfBooks[key].sort()
        for i in range (0, len(noOfBooks[key])):
            print "    ", noOfBooks[key][i]
        print
else:
    # ------ Print book list alphabetically by author -----
    keys = authors.keys()
    keys.sort()
    for author in keys:
        print author
        titles = authors[author].sort()
        for title in authors[author]:
            print "    %s" % title

Here's the results.

I actually took it one step further. If you set

printSummary = False

near the top of the script, it'll generate an alphabetical book list by author which you can then print out and easily use when you visit a bookstore. As they say on all the best cooking shows and to save you the trouble, I already have one prepared.

If I was to become even more obsessed with this and wanted to take it further, I'd wonder:

But that's enough for now.

[]

( Sep 16 2007, 10:09:59 AM PDT ) [Listen] Permalink Comments [6]