Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: get each pair from a string.

Reply
Thread Tools

Re: get each pair from a string.

 
 
Emile van Sebille
Guest
Posts: n/a
 
      10-22-2012
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



 
Reply With Quote
 
 
 
 
rusi
Guest
Posts: n/a
 
      10-22-2012
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']
>>>

 
Reply With Quote
 
 
 
 
rusi
Guest
Posts: n/a
 
      10-23-2012
On Oct 22, 9:19*pm, rusi <(E-Mail Removed)> 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]
 
Reply With Quote
 
wxjmfauth@gmail.com
Guest
Posts: n/a
 
      10-23-2012
Le mardi 23 octobre 2012 06:59:49 UTC+2, rusi a écrit*:
> On Oct 22, 9:19*pm, rusi <(E-Mail Removed)> 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
 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      10-23-2012
On 2012-10-23, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      10-23-2012
On 23/10/2012 07:47, (E-Mail Removed) 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.

 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      10-23-2012
On Tue, Oct 23, 2012 at 12:47 AM, <(E-Mail Removed)> 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.
 
Reply With Quote
 
wxjmfauth@gmail.com
Guest
Posts: n/a
 
      10-24-2012
Le mardi 23 octobre 2012 18:41:45 UTC+2, Ian a écrit*:
> On Tue, Oct 23, 2012 at 12:47 AM, <(E-Mail Removed)> 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
 
Reply With Quote
 
wxjmfauth@gmail.com
Guest
Posts: n/a
 
      10-24-2012
Le mardi 23 octobre 2012 18:41:45 UTC+2, Ian a écrit*:
> On Tue, Oct 23, 2012 at 12:47 AM, <(E-Mail Removed)> 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
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      10-24-2012
On 24/10/2012 10:32, (E-Mail Removed) wrote:
> Le mardi 23 octobre 2012 18:41:45 UTC+2, Ian a écrit :
>> On Tue, Oct 23, 2012 at 12:47 AM, <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: get each pair from a string. Emile van Sebille Python 0 10-22-2012 07:07 AM
Re: get each pair from a string. Mark Lawrence Python 0 10-21-2012 07:05 PM
Re: get each pair from a string. Ian Kelly Python 0 10-21-2012 06:51 PM



Advertisments