wordnet semantic similarity: how to refer to elements of a pair in a list? can we sort dictionary according to the value?

alex23 wuwei23 at gmail.com
Tue Oct 9 01:44:04 EDT 2012


On Oct 9, 2:16 pm, Token Type <typeto... at gmail.com> wrote:
> When I try my above codes, what puzzles me is that when
> the data in the dictionary increase, some data become
> missing in the sorted result. Quite odd. In the pairs,
> we have {'journey':'voyage'} but in the sorted result no (
> 'journey-voyage',0.25)
>
> >>> pairs = {'car':'automobile', 'gem':'jewel', 'journey':'voyage','boy':'lad','coast':'shore', 'asylum':'madhouse', 'magician':'wizard', 'midday':'noon', 'furnace':'stove', 'food':'fruit', 'bird':'cock', 'bird':'crane', 'tool':'implement', 'brother':'monk', 'lad':'brother', 'crane':'implement', 'journey':'car', 'monk':'oracle', 'cemetery':'woodland', 'food':'rooster', 'coast':'hill', 'forest':'graveyard', 'shore':'woodland', 'monk':'slave', 'coast':'forest','lad':'wizard', 'chord':'smile', 'glass':'magician', 'rooster':'voyage', 'noon':'string'}

Keys are unique in dictionaries. You have two uses of 'journey'; the
second will overwrite the first.

Do you _need_ these items to be a dictionary? Are you doing any look
up? If not, just make it a list of tuples:

   pairs = [ ('car', 'automobile'), ('gem', 'jewel') ...]

Then make your main loop:

   for word1, word2 in pairs:

If you do need a dictionary for other reasons, you might want to try a
dictionary of lists:

    pairs = {
        'car': ['automobile', 'vehicle'],
        'gem': ['jewel'],
    }

    for word1, synonyms in pairs:
        for word2 in synonyms:
            ...



More information about the Python-list mailing list