Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106285

Re: Drowning in a teacup?

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Newsgroups comp.lang.python
Subject Re: Drowning in a teacup?
Date 2016-04-02 12:53 +0100
Message-ID <mailman.362.1459598050.28225.python-list@python.org> (permalink)
References <ndmlj2$pb0$1@gioia.aioe.org> <ndnm9v$j07$1@ger.gmane.org> <CAGgTfkO4HuzoRBgrGJ3ccqcD+VB4_7O-g19NUk9FaYSBpQ2HkA@mail.gmail.com>

Show all headers | View raw


On 02/04/2016 06:51, Michael Selik wrote:
> On Sat, Apr 2, 2016, 1:46 AM Vito De Tullio <vito.detullio@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Drowning in a teacup? Fillmore <fillmore_remove@hotmail.com> - 2016-04-01 16:27 -0400
  Re: Drowning in a teacup? Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-01 20:39 +0000
    Re: Drowning in a teacup? Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-01 14:52 -0600
  Re: Drowning in a teacup? MRAB <python@mrabarnett.plus.com> - 2016-04-01 21:45 +0100
  Re: Drowning in a teacup? Michael Selik <michael.selik@gmail.com> - 2016-04-01 20:46 +0000
  Re: Drowning in a teacup? Steven D'Aprano <steve@pearwood.info> - 2016-04-02 09:46 +1100
  Re: Drowning in a teacup? Fillmore <fillmore_remove@hotmail.com> - 2016-04-02 00:03 -0400
  Re: Drowning in a teacup? Vito De Tullio <vito.detullio@gmail.com> - 2016-04-02 07:45 +0200
  Re: Drowning in a teacup? Michael Selik <michael.selik@gmail.com> - 2016-04-02 05:51 +0000
  Re: Drowning in a teacup? Vito De Tullio <vito.detullio@gmail.com> - 2016-04-02 08:26 +0200
  Re: Drowning in a teacup? Peter Otten <__peter__@web.de> - 2016-04-02 12:31 +0200
  Re: Drowning in a teacup? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-04-02 12:53 +0100
  Re: Drowning in a teacup? Michael Selik <michael.selik@gmail.com> - 2016-04-02 14:36 +0000
  Re: Drowning in a teacup? Ned Batchelder <ned@nedbatchelder.com> - 2016-04-02 12:28 -0700
    Re: Drowning in a teacup? Random832 <random832@fastmail.com> - 2016-04-02 15:54 -0400
      Re: Drowning in a teacup? Steven D'Aprano <steve@pearwood.info> - 2016-04-03 12:33 +1000
    Re: Drowning in a teacup? Ethan Furman <ethan@stoneleaf.us> - 2016-04-02 16:15 -0700
    Re: Drowning in a teacup? Random832 <random832@fastmail.com> - 2016-04-02 19:19 -0400

csiph-web