Newbie data structes question

Peter Otten __peter__ at
Thu Dec 4 15:34:27 CET 2003

Kamus of Kadizhar wrote:

> I have never programmed anyting in python, so this is completely new
> territory.  I've programmed in C for so long it's pretty hard wired in
> my brain.  I'm trying to break out :-)
> I have a program in mind that I think would be a good learning project.
> I have a machine that shows movies.  There are favorites (most often
> watched movies) that are kept on the machine and archives that are kept
> on an NFS server.  There are also new arrivals.  All movies (new
> arrivals, favorites, etc) are in the archives.
> The machine logs movie names of all movies played.  The log file is
> simply a list of movie titles played.  If a movie has been played 10
> times, it will appear in the list 10 times.
> I want to write a script that will automatically move most watched
> movies into favorites, delete movies that are no longer favorites, and
> replace them with movies that are more frequently watched from archives.
> So, I have to create a paired list of some sort:
> (movie, count), (movie, count), (movie, count) ....

allmovies = {} # title -> count dictionary
for movie in file("watched.log"):
    # remove leading/trailing whitespace
    movie = movie.strip()
    # count movie
    allmovies[movie] = allmovies.get(movie, 0) + 1

# list of (title, freq) tuples
favourites = allmovies.items()

> sort the list on the counts, and then loop through from most often
> watched movie not in favorites replacing it in the favorites list with
> the least often watched movie in the favorites list.  Stop looping when
> the watch count for both in favorites and not in favorites is the same.

# sort descending by frequency
def descFreqCompare(m1, m2):
    return cmp(m2[1], m1[1])

# remove all non-favourites
del favourites[10:]

topten = [movie for movie, freq in favourites]

> If any movies put in to the favorites were in the new arrivals, delete
> from new arrivals.

import sets
# read new arrivals
newarrivals = sets.Set([movie.strip() for movie in file("newarrivals.log")])
newarrivals -= sets.Set(topten)
outstream = file("newarrivals.log", "w")
for movie in newarrivals:
    print >> outstream, movie

> So, here's my python question:

Oops, I answered what you didn't ask :-)

> What data structures and flow controls are most appropriate?  Any neat
> flow control in python?

Let's see - there was a nice for loop, and then we have list comprehensions
which boil down to for loops in [...].
Seriously, most work is done with Python's powerful list/dictionary
implementations. I you already have some programming practice, the Python
tutorial at is a good
starting point. This together with the library documentation and perhaps
Alex Martelli's "Python in a Nutshell" is all you need to actually write
most of the programs you only dreamed of writing in C.


More information about the Python-list mailing list