[Tutor] Parsing

Andreas Perstinger andreas.perstinger at gmx.net
Mon Nov 28 00:50:10 CET 2011


[Please reply to the list. Your indentation also got lost during the 
mail delivery.]

On 2011-11-27 23:21, Deanna Wilson wrote:
> Yes it is homework, but not from Penn state. It is a Geog690 class. I'm
> having difficulties with determining where the rhino is referenced in the
> split line, determining if the dictionary has a key for the rhino and if no
> key exists, creating a new array object. So pretty much writing the
> dictionary. I think I got the rest of the script just not understanding the
> dictionary portion. I would appreciate any help/advice.
>
> Here is part of my script where I tried to create a dictionary
>
> rhinoLocalDictionary = {}
>
> def rhinoName(Rhino, Lat, Lon, dictionary):
> if rhinoName in dictionary:
> dictionary[rhinoName].append([Lat, Lon])
> else:
> dictionary[rhinoName]= ([Lat, Lon])

You define the function "rhinoName" with the parameter "Rhino" but 
inside the function you use "rhinoName" which is the function's name.

You want to build a list of lists for each dictionary-value. But then 
you need to start the list with a nested-list in the else-branch. 
Otherwise your list will start with two elements followed by 
two-elements lists:

 >>> d = {}
 >>> d[1] = [1, 2]
 >>> d[1].append([3, 4])
 >>> d[1]
[1, 2, [3, 4]]     # That's not what you want
 >>> d[2] = [[1, 2]]
 >>> d[2].append([3, 4])
 >>> d[2]
[[1, 2], [3, 4]]   # Better

But assuming that your lat/lon-values don't change I would suggest using 
tuples.

HTH, Andreas


More information about the Tutor mailing list