Newbie data structes question

  • Thread starter Kamus of Kadizhar
  • Start date
K

Kamus of Kadizhar

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) ....

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.

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

So, here's my python question:

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

Thanks,

-Kamus
 
P

Peter Hansen

Kamus said:
So, here's my python question:

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

Nope. Python is built on a small set of entirely uninteresting
control structures, designed to lead you into a state of mind where
you think more about the logic of your code than about whether you
can whip something off as a one-liner or obfuscate it to show your
brilliance in comparison to lesser programmers. ;-)

-Peter
 
D

Diez B. Roggisch

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

This should do the counting and sorting

def cmp(m1, m2):
if m1[1] < m2[1]:
return -1
elif m1[1] > m2[1]:
return 1
return 0

# you can create your own file here
in_file = sys.stdin
lines = in_file.realines()
# sorts the lines in place
lines.sort()

res = {}
for line in lines:
if res.has_key(line):
res[line] += 1
else:
res[line] = 1

res = res.items()
res.sort(cmp)

res now has this form:

[('brazil', 20), ('cross roads', 0)]

Thats it. In python, you have basically three data structures - tuples
(1,2,3), lists [1,2,3] and dicts {'key' : 'value', 'key2', 3}

You don't have to declare any of them :)

Regards,

Diez
 
P

Peter Otten

Kamus said:
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
 
K

Kamus of Kadizhar

Seriously, most work is done with Python's powerful list/dictionary
implementations.

Thanks for all the help from everyone. I've done programming (tons and
tons) in C, some in lisp, and gobs years ago in FORTRAN and SNOBOL. It's
always interesting to come across new ideas; I'm going to have to chew on
the little snippets of code pasted here until I can digest them. Those
new ideas hurt until they are assimilated....

:)

-Kamus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,473
Latest member
ChristelPe

Latest Threads

Top