hash tables in python?

Remco Gerlich scarblac at pino.selwerd.nl
Mon May 21 04:21:27 EDT 2001


apark at cdf.toronto.edu <apark at cdf.toronto.edu> wrote in comp.lang.python:
> 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"
> 	}


users1 = {}
[some loop that goes through a file]
   if not users1.has_key(username): # Can't append to undefined key, so test
      users1[username] = ""
   users1[username] += "  %s %s %s %s  %s\n" % (month, date, day, time, file)

items = users1.items()  # We use both keys and values
items.sort()            # Sort sorts inplace so we need this temp variable
for username, value in items:
   print username
   print value


-- 
Remco Gerlich



More information about the Python-list mailing list