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

Ian Kelly ian.g.kelly at gmail.com
Mon Oct 8 23:24:46 EDT 2012


On Mon, Oct 8, 2012 at 9:13 PM, Token Type <typetoken at gmail.com> wrote:
> yes, thanks all your tips. I did try sorted with itemgetter. However, the sorted results are same as follows whether I set reverse=True or reverse= False. Isn't it strange? Thanks.

First of all, "sorted" does not sort the list in place as you seem to
be expecting.
It returns a new sorted list.  Since your code does not store the
return value of the sorted call anywhere, the sorted list is discarded
and only the original list is kept.  If you want to sort a list in
place, use the list.sort method instead.

Second, you're not sorting the overall list.  On each iteration your
code: 1) assigns a new empty list to list_simi; 2) processes one of
the pairs; 3) adds the pair to the empty list; and 4) sorts the list.
On the next iteration you then start all over again with a new empty
list, and so when you get to the sorting step you're only sorting one
item each time.  You need to accumulate the list instead of wiping it
out on each iteration, and only sort it after the loop has completed.



More information about the Python-list mailing list