[SOLVED] Python: Sorting a List of Strings


Results 1 to 4 of 4

Thread: [SOLVED] Python: Sorting a List of Strings

  1. #1
    Join Date
    May 2003
    Location
    McMinnville, Oregon, USA
    Posts
    502

    [SOLVED] Python: Sorting a List of Strings

    I've got a directory full of files named "00001", "00002", etc. In Python, I get the list of files in this directory via os.listdir(theDir). However, this list is totally out of order. In fact, I can't tell what order it's in.

    I want to iterate through this list of files in order (1 first, 2 second...). I've tried listdir().sort(), listdir().sort(key=str.lower), and a couple other things, including lst = os.listdir(dir); lst.sort().

    Any ideas?

  2. #2
    Join Date
    May 2003
    Location
    McMinnville, Oregon, USA
    Posts
    502
    Oi.... I spent all night trying to figure this out. Then I posted this question, and then I figured out where my typo was.

    I'm sorry. Please, mod me down or something

  3. #3
    Join Date
    Apr 2001
    Location
    Nashville, TN
    Posts
    3,198
    Hey, it's cool. Mind posting your code, for future reference?
    Registered Linux user #230403! Since March 2001! YAY.

    Try doing a forum search or a google search before asking a question. And please don't use HELP! in the topic of your post... it's so lame... Please don't PM me for help-- post a question in the forum instead.

  4. #4
    Join Date
    May 2003
    Location
    McMinnville, Oregon, USA
    Posts
    502
    In my specific case, I'm making blog software. I want to get the latest 5 blog posts I've made and put them on my homepage. Each blog entry is each kept as an individual file whose name is a number that indicates its place in time among my other entries (00001 came first, and currently, 00020 is the latest). It'd probably be better to implement the file names as time stamps or seconds since epoch, but this is all toy code.

    So to get the 5 latest entries, I start by making a list of files, then sorting it alphabetically, and finally reversing that list so the first item in it is my latest blog entry. The following code is in Python:
    Code:
    blogPosts = os.listdir("blog/posts") #Gets the list of files.
    blogPosts.sort() #Puts the list in alphabetical order.
    blogPosts.reverse() #Sorts the list so that newer blog entries
                        #appear at the beginning of the list.
    Now, to actually display those blog files, I use the following code. It iterates through the first 5 blog entries and prints their file contents to stdout (which, in this case, is the browser viewing the results of this CGI script).
    Code:
    for entry in blogPosts[:5]:
    	post = open("blog/posts/" + entry)
    	for x in post.readlines():
    		print x
    Note that the blog entries themselves are just files with HTML. Specifically, I use the following tag for entry titles: <h3 class="blog_title">. Then comes the date, which is just <p class="blog_date">, followed by the entry, which is <p class="blog_entry">. So an entry might look like this:

    Code:
    <h3 class="blog_title">Tongue Twister</h3>
    <p class="blog_date">2006-1-29</p>
    <p class="blog_entry">We'll weather the weather, whatever the weather,
    whether we like it or not!</p>
    In this way, I can control the exact look of my blog via CSS and, as outlined above, display entries simply by printing the contents of the file to a web page.

    For better implementation, the number of blog entries iterated through could be determined by a variable, so the first line of code in that last code listing would look more like this:

    Code:
    for entry in blogPosts[:customNumberOfPosts]:
    ...
    That's the long-form explanation! Maybe if I can find the time, I'll write a full tutorial. Figuring out how to write proper blog software has been trying, since I've done it all in my meager spare time over several months, but the end result is simple. If I could just have 5 contiguous hours to sit down and work on this, I could've hammered this thing out in one sitting months ago.

    It's similarly simple to implement a secure page for posting blog entries. I'm assuming my idea for searching entries will also be simple, though it might get slow after enough entries have been made.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •