Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed5.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'happen?': 0.09; 'subject:method': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; 'subject:python': 0.11; 'iteration': 0.16; 'operation.': 0.16; 'statement.': 0.16; '>>>': 0.18; 'variable': 0.20; 'thanks.': 0.21; 'runs': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'separate': 0.27; 'subject:list': 0.28; 'directly,': 0.29; 'probably': 0.29; 'skip:s 30': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'faster': 0.35; 'skip:l 30': 0.35; 'doing': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'instead': 0.39; "you'll": 0.62; 'subject:there': 0.65 Newsgroups: comp.lang.python Date: Sat, 8 Sep 2012 23:44:59 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=221.127.246.219; posting-account=mbqf2AoAAAAgAZURfwG566lEnhttS2f1 References: <60A480B7378343149401A424A682AF34@gmail.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 221.127.246.219 MIME-Version: 1.0 Subject: Re: Is there a unique method in python to unique a list? From: Token Type To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: , Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347173102 news.xs4all.nl 6952 [2001:888:2000:d::a6]:37825 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28757 > Try backdenting that statement. You're currently doing it at every > > iteration of the loop - that's why it's so much slower. Thanks. I works now. >>> def average_polysemy(pos): synset_list = list(wn.all_synsets(pos)) sense_number = 0 lemma_list = [] for synset in synset_list: lemma_list.extend(synset.lemma_names) for lemma in list(set(lemma_list)): sense_number_new = len(wn.synsets(lemma, pos)) sense_number = sense_number + sense_number_new return sense_number/len(set(lemma_list)) >>> average_polysemy('n') 1 > But you'll probably find it better to work with the set directly, > > instead of uniquifying a list as a separate operation. Yes, the following second methods still runs faster if I don't give a separate variable name to list(set(lemma_list)). Why will this happen? >>> def average_polysemy(pos): synset_list = list(wn.all_synsets(pos)) sense_number = 0 lemma_list = [] for synset in synset_list: lemma_list.extend(synset.lemma_names) for lemma in list(set(lemma_list)): sense_number_new = len(wn.synsets(lemma, pos)) sense_number = sense_number + sense_number_new return sense_number/len(set(lemma_list)) >>> average_polysemy('n') 1