hash tables in python?

apark at cdf.toronto.edu apark at cdf.toronto.edu
Mon May 21 00:00:11 EDT 2001


Hi,

I'm trying to translate the following Perl code to Python

	[some loop that goes through a file]
	{
	   $users1{$username} .= "  $month $date $day $time  $file\n";
	}

	foreach $username (sort keys %users1) {
	        $tmp = $users1{$username};

		print "$username"
		print "$tmp"
	}

This is as short as I can present it from the original code.  The way
I have this implemented in Python is that I use two arrays.
One to keep track of the user name, another one to keep the data in
the corresponding slot in the array, I.E.

	user1=[]
	user1_index=[]
	[some loop]:
		[some how get user's name into username]
		try:
			h=user1_index.index(username)
			user1[h]= feed the data here
		except ValueError:
			user1_index.append(username)

	for i in range(len(user1_index)):
		print user1_index[i] + "\n" + user1[i] + "\n"

I'm thinking that there has to be a better way to program this...
Am I wrong?
Thanks in advance.

-A.




More information about the Python-list mailing list