Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.034 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'false.': 0.07; '9:13': 0.16; 'assigns': 0.16; 'discarded': 0.16; 'iteration': 0.16; 'iteration,': 0.16; 'list;': 0.16; 'oct': 0.16; 'sorting': 0.16; 'sorts': 0.16; 'subject: \n ': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'sort': 0.21; 'all,': 0.21; 'thanks.': 0.21; 'header:In-Reply- To:1': 0.25; 'instead.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'second,': 0.29; 'received:209.85.215.46': 0.30; 'code': 0.31; 'to:addr:python-list': 0.33; 'code:': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'list': 0.35; 'adds': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'list.': 0.35; 'method': 0.36; 'does': 0.37; 'item': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'step': 0.39; 'list,': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'first': 0.61; 'results': 0.65; 'overall': 0.66; 'subject:value': 0.84; 'to:name:python': 0.84; 'anywhere,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=TLIz4Ggae4ZsxLq5CW8l6fOPde4U1WD3vi1py/gn/IY=; b=huW3SXXp5/2s3o6u66IAS2ZDKoA+qw6qncSyz12Cm0A6ej8ryvMicpveKwL4lz9q/S k7nlu9840240R4qQ1SizCgQyRsnMA4xNZtHtSFcIW0R5P6DrwAljh2y8il9Tv8QKIedL YAPQ9NH01B++lhuButln8vwR5FHHXUlKpb1lWQLuPeXpJ0grt6bfsR7uNvJ40iipslk6 f4DjlfcDAjJbW04LAYBC1GdfxcHwjjK73y3b+SUsxUxBq2CChav5RhJXH8yyj0VXIEgB AquYwcoUUQ4qxKdqsa5kI4Y8mzjZOGbUMJ/yljypGiHZ2Al5pAVujNr1QOVykyL77Mz6 kzKQ== MIME-Version: 1.0 In-Reply-To: <62e7500b-0d7d-4d5e-8c10-197d1988f364@googlegroups.com> References: <2d6d84d4-0f70-4280-96e2-f9fe17d5be8b@googlegroups.com> <62e7500b-0d7d-4d5e-8c10-197d1988f364@googlegroups.com> From: Ian Kelly Date: Mon, 8 Oct 2012 21:24:46 -0600 Subject: Re: wordnet semantic similarity: how to refer to elements of a pair in a list? can we sort dictionary according to the value? To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 17 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349753118 news.xs4all.nl 6880 [2001:888:2000:d::a6]:35135 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30993 On Mon, Oct 8, 2012 at 9:13 PM, Token Type 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.