Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Mark Lawrence Newsgroups: comp.lang.python Subject: Re: Drowning in a teacup? Date: Sat, 2 Apr 2016 12:53:49 +0100 Lines: 43 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de PkzLNkdxCmumYtXN+4tjOA0LRgRUGBk1CS8lxZhfyamg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.05; 'matches': 0.07; 'strings.': 0.07; 'wrapper': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'suggestions.': 0.09; 'python': 0.10; 'iirc': 0.16; 'key?': 0.16; 'keyword,': 0.16; 'list)': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'element': 0.18; '(in': 0.18; 'language': 0.19; '>>>': 0.20; 'latter': 0.22; 'lawrence': 0.22; 'elements': 0.23; 'sat,': 0.23; 'url:profile': 0.23; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'function': 0.28; '(new': 0.29; 'measure': 0.29; 'relative': 0.30; "i'd": 0.31; 'post': 0.31; "can't": 0.32; 'language.': 0.32; 'url:python': 0.33; 'michael': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'list': 0.34; 'something': 0.35; 'list,': 0.36; 'there': 0.36; 'url:org': 0.36; 'beginning': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:org': 0.37; 'list.': 0.37; 'things': 0.38; 'front': 0.38; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'called': 0.40; 'url:3': 0.60; 'charset:windows-1252': 0.62; 'our': 0.64; 'feeling': 0.72; 'gut': 0.84; 'pythonistas,': 0.84; 'snap': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 80.234.189.93 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.1 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:106285 On 02/04/2016 06:51, Michael Selik wrote: > On Sat, Apr 2, 2016, 1:46 AM Vito De Tullio wrote: > >> Fillmore wrote: >> >>> I need to scan a list of strings. If one of the elements matches the >>> beginning of a search keyword, that element needs to snap to the front >>> of the list. >> >> I know this post regards the function passing, but, on you specific >> problem, >> can't you just ... sort the list with a custom key? >> >> something like (new list) >> >> >>> sorted(['no_a', 'yes_c', 'no_b', 'yes_z', 'no_y', 'yes_x'], >> ... key=lambda e: not e.startswith('yes')) >> ['yes_c', 'yes_z', 'yes_x', 'no_a', 'no_b', 'no_y'] >> >> or (in place) >> >> >>> l = ['no_a', 'yes_c', 'no_b', 'yes_z', 'no_y', 'yes_x'] >> >>> l.sort(key=lambda e: not e.startswith('yes')) >> >>> l >> ['yes_c', 'yes_z', 'yes_x', 'no_a', 'no_b', 'no_y'] >> > > If the number of matches is small relative to the size of the list, I'd > expect the sort would be slower than most of the other suggestions. > My gut feeling about how fast something is in Python has never once been right in 15 years. There is only way way to find out, measure it with things like https://docs.python.org/3/library/profile.html and https://docs.python.org/3/library/timeit.html. IIRC Steven D'Aprano has a wrapper for the latter called Stopwatch. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence