Re: get each pair from a string.

Discussion in 'Python' started by Emile van Sebille, Oct 22, 2012.

  1. On 10/21/2012 11:33 AM, Vincent Davis wrote:
    > I am looking for a good way to get every pair from a string. For example,
    > input:
    > x = 'apple'
    > output
    > 'ap'
    > 'pp'
    > 'pl'
    > 'le'
    >
    > I am not seeing a obvious way to do this without multiple for loops, but
    > maybe there is not :)
    > In the end I am going to what to get triples, quads....... also.
    >


    How far have you gotten? Show us the loops you're trying now and any
    errors you're getting.

    Emile
    Emile van Sebille, Oct 22, 2012
    #1
    1. Advertising

  2. Emile van Sebille

    rusi Guest

    On 10/21/2012 11:33 AM, Vincent Davis wrote:

    > I am looking for a good way to get every pair from a string. For example,
    > input:
    > x = 'apple'
    > output
    > 'ap'
    > 'pp'
    > 'pl'
    > 'le'


    Maybe zip before izip for a noob?

    >>> s="apple"
    >>> [a+b for a,b in zip(s, s[1:])]

    ['ap', 'pp', 'pl', 'le']
    >>>
    rusi, Oct 22, 2012
    #2
    1. Advertising

  3. Emile van Sebille

    rusi Guest

    On Oct 22, 9:19 pm, rusi <> wrote:
    > On 10/21/2012 11:33 AM, Vincent Davis wrote:
    >
    > > I am looking for a good way to get every pair from a string. For example,
    > > input:
    > > x = 'apple'
    > > output
    > > 'ap'
    > > 'pp'
    > > 'pl'
    > > 'le'

    >
    > Maybe zip before izip for a noob?
    >
    > >>> s="apple"
    > >>> [a+b for a,b in zip(s, s[1:])]

    >
    > ['ap', 'pp', 'pl', 'le']


    Daniel wrote:
    > This is a little bit faster:
    >
    > s = "apple"
    > [s[i:i+2] for i in range(len(s)-1)]


    Nice! I always find pairs of structural decomposition of input vs
    recomposition of output interesting.
    In this case the use of slices:
    to decompose: s -> s[1:]
    vs doing s[i:i+2]
    rusi, Oct 23, 2012
    #3
  4. Emile van Sebille

    Guest

    Le mardi 23 octobre 2012 06:59:49 UTC+2, rusi a écrit :
    > On Oct 22, 9:19 pm, rusi <> wrote:
    >
    > > On 10/21/2012 11:33 AM, Vincent Davis wrote:

    >
    > >

    >
    > > > I am looking for a good way to get every pair from a string. For example,

    >
    > > > input:

    >
    > > > x = 'apple'

    >
    > > > output

    >
    > > > 'ap'

    >
    > > > 'pp'

    >
    > > > 'pl'

    >
    > > > 'le'

    >
    > >

    >
    > > Maybe zip before izip for a noob?

    >
    > >

    >
    > > >>> s="apple"

    >
    > > >>> [a+b for a,b in zip(s, s[1:])]

    >
    > >

    >
    > > ['ap', 'pp', 'pl', 'le']

    >
    >
    >
    > Daniel wrote:
    >
    > > This is a little bit faster:

    >
    > >

    >
    > > s = "apple"

    >
    > > [s[i:i+2] for i in range(len(s)-1)]

    >
    >
    >
    > Nice! I always find pairs of structural decomposition of input vs
    >
    > recomposition of output interesting.
    >
    > In this case the use of slices:
    >
    > to decompose: s -> s[1:]
    >
    > vs doing s[i:i+2]


    Why bother with speeed?

    The latest Python version is systematically slower
    than the previous ones as soon as one uses non "ascii
    strings".

    Python users are discussing "code optimizations" without
    realizing the tool they are using, has killed itself its
    own performances.

    (Replace 'apple' with 'ap需')

    jmf
    , Oct 23, 2012
    #4
  5. Emile van Sebille

    Neil Cerutti Guest

    On 2012-10-23, <> wrote:
    > Why bother with speeed?


    Because the differences between O(N), O(log(N)) and O(N ** 2)
    operations are often relevant.

    A Python string replace function experiencing a slow-down from
    previous versions doesn't absolve me from making informed choices
    of algorithm and implentation.

    --
    Neil Cerutti
    Neil Cerutti, Oct 23, 2012
    #5
  6. On 23/10/2012 07:47, wrote:
    >
    > Why bother with speeed?
    >
    > The latest Python version is systematically slower
    > than the previous ones as soon as one uses non "ascii
    > strings".
    >
    > Python users are discussing "code optimizations" without
    > realizing the tool they are using, has killed itself its
    > own performances.
    >
    > (Replace 'apple' with 'ap需')
    >
    > jmf
    >


    Please stop giving blatant lies on this list about Python speed.

    --
    Cheers.

    Mark Lawrence.
    Mark Lawrence, Oct 23, 2012
    #6
  7. Emile van Sebille

    Ian Kelly Guest

    On Tue, Oct 23, 2012 at 12:47 AM, <> wrote:
    > The latest Python version is systematically slower
    > than the previous ones as soon as one uses non "ascii
    > strings".


    No, it isn't. You've previously demonstrated a *microbenchmark* where
    3.3 is slower than 3.2. This is a far cry from demonstrating that 3.3
    is "systematically slower", and that larger claim is simply false.

    In any case, have you been following the progress of issue 16061?
    There is a patch for the str.replace regression that you identified,
    which results in better performance across the board than 3.2. So
    although 3.3 is slower than 3.2 on this one microbenchmark, 3.4 will
    not be.
    Ian Kelly, Oct 23, 2012
    #7
  8. Emile van Sebille

    Guest

    Le mardi 23 octobre 2012 18:41:45 UTC+2, Ian a écrit :
    > On Tue, Oct 23, 2012 at 12:47 AM, <> wrote:
    >
    >
    >
    > In any case, have you been following the progress of issue 16061?
    >
    > There is a patch for the str.replace regression that you identified,
    >
    > which results in better performance across the board than 3.2. So
    >
    > although 3.3 is slower than 3.2 on this one microbenchmark, 3.4 will
    >
    > not be.


    Yes.
    A workaround to solve one of the corner cases of something
    which is wrong by design.

    jmf
    , Oct 24, 2012
    #8
  9. Emile van Sebille

    Guest

    Le mardi 23 octobre 2012 18:41:45 UTC+2, Ian a écrit :
    > On Tue, Oct 23, 2012 at 12:47 AM, <> wrote:
    >
    >
    >
    > In any case, have you been following the progress of issue 16061?
    >
    > There is a patch for the str.replace regression that you identified,
    >
    > which results in better performance across the board than 3.2. So
    >
    > although 3.3 is slower than 3.2 on this one microbenchmark, 3.4 will
    >
    > not be.


    Yes.
    A workaround to solve one of the corner cases of something
    which is wrong by design.

    jmf
    , Oct 24, 2012
    #9
  10. On 24/10/2012 10:32, wrote:
    > Le mardi 23 octobre 2012 18:41:45 UTC+2, Ian a écrit :
    >> On Tue, Oct 23, 2012 at 12:47 AM, <> wrote:
    >>
    >>
    >>
    >> In any case, have you been following the progress of issue 16061?
    >>
    >> There is a patch for the str.replace regression that you identified,
    >>
    >> which results in better performance across the board than 3.2. So
    >>
    >> although 3.3 is slower than 3.2 on this one microbenchmark, 3.4 will
    >>
    >> not be.

    >
    > Yes.
    > A workaround to solve one of the corner cases of something
    > which is wrong by design.
    >
    > jmf
    >


    Do you get paid money for acting as a complete moron or do you do it for
    free?

    --
    Cheers.

    Mark Lawrence.
    Mark Lawrence, Oct 24, 2012
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Kelly

    Re: get each pair from a string.

    Ian Kelly, Oct 21, 2012, in forum: Python
    Replies:
    0
    Views:
    134
    Ian Kelly
    Oct 21, 2012
  2. Mark Lawrence

    Re: get each pair from a string.

    Mark Lawrence, Oct 21, 2012, in forum: Python
    Replies:
    0
    Views:
    155
    Mark Lawrence
    Oct 21, 2012
  3. Emile van Sebille

    Re: get each pair from a string.

    Emile van Sebille, Oct 22, 2012, in forum: Python
    Replies:
    0
    Views:
    186
    Emile van Sebille
    Oct 22, 2012
  4. Ian Kelly

    Re: get each pair from a string.

    Ian Kelly, Oct 21, 2012, in forum: Python
    Replies:
    0
    Views:
    163
    Ian Kelly
    Oct 21, 2012
  5. Emile van Sebille

    Re: get each pair from a string.

    Emile van Sebille, Oct 22, 2012, in forum: Python
    Replies:
    0
    Views:
    169
    Emile van Sebille
    Oct 22, 2012
Loading...

Share This Page