Some pythonic advice needed

Andrei see at my.signature.com
Sun Jun 1 15:10:09 EDT 2003


Originally posted by Rudy Schockaert 
> It is the reverse translation that makes it difficult for me.
> <snip>
> 

Here's a quick interactive session for reversing the dictionary

>>> lat_dut = {"iste": ["dat", "die"], "ille": ["dat", "die"], "vere":
>>> ["waar"]}
>>> dut_lat = {}
>>> for latinword in lat_dut.keys():
..     for dutchword in lat_dut[latinword]:
..         if dut_lat.has_key(dutchword):
..             dut_lat[dutchword].append(latinword)
..         else:
..             dut_lat[dutchword]=[latinword]
..
>>> print dut_lat
{'dat': ['ille', 'iste'], 'die': ['ille', 'iste'], 'waar': ['vere']}

It's not clear to me how you define the "expected" answer. Would you say
"iste" would suppose to return "die" or "dat"? (I speak Dutch and I know
they mean the same, so none is better or worse than the other). Perhaps
you should consider a different approach, e.g. allow to give more than
one answer and point it out on-screen when not all possibilities have
been exhausted. Or group equivalent answers, perhaps like this
interactive session:

>>> class Answer(object):
..     def __init__(self, *args):
..         """The args are a list of possible answers"""
..         self.words = args[:]
..         self.score = 0
..     def __str__(self):
..         return "%s: %s" % (self.words, self.score)
..
>>> for translation in lat_dut["iste"]: print translation
..
('dat', 'die'): 0

>>> for translation in lat_dut["ubi"]: print translation
..
('waar',): 0
('wanneer',): 0

As you see, "dat" and "die" are grouped here and have a common score as
both are equivalent, while "waar" and "wanneer" have separate scores as
they are different words.

--
print "znvy: cebwrpg5 at bcrenznvy.pbz
Fcnzserr! Cyrnfr qb abg hfr va choyvp zrffntrf. V ernq gur yvfg, ab arrq gb PP.".decode("rot13")


Posted via http://dbforums.com




More information about the Python-list mailing list