Re: Newbie data structes question

From: Peter Otten (__peter___at_web.de)
Date: 12/04/03


Date: Thu, 04 Dec 2003 15:34:27 +0100

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])
favourites.sort(descFreqCompare)

# 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
outstream.close()

>
> 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 http://www.python.org/doc/current/tut/tut.html 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.

Peter



Relevant Pages

  • Re: Favorite electronics movies
    ... > One of my favorites was on tv tonight - Enemy of the State. ... Is it ever *subversive* to simply show a movie! ... report ABC to the comittee for unAmerican activities? ...
    (sci.electronics.design)
  • Re: How Very Interesting!
    ... general excellence of his oeuvre. ... though it's not one of my personal favorites. ... the movie, which was only fair as movies go but ... Night Watch is a damned good book -- but I think you have to have ...
    (rec.arts.sf.written)
  • Re: Favorite electronics movies
    ... >> Does anyone here think it was subversive of ABC to schedule this movie ... > report ABC to the comittee for unAmerican activities? ... One of my all time favorites. ...
    (sci.electronics.design)
  • Re: (OT)- Your Fav Flix
    ... sophisticated, media darling" movie lover, not that there's anything ... It's NOT fun seeing other ... people's favorites these days.) ...
    (rec.arts.movies.current-films)
  • Re: (OT)- Your Fav Flix
    ... sophisticated, media darling" movie lover, not that there's anything ... It's NOT fun seeing other ... people's favorites these days.) ...
    (rec.arts.movies.current-films)