Re: Splitting of string at an interval

2013-04-09 Thread Dave Angel
On 04/08/2013 10:38 PM, Steven D'Aprano wrote: On Mon, 08 Apr 2013 21:09:08 -0400, Roy Smith wrote: There's a whole competition about writing the smallest program which outputs the song 99 bottles of beer: http://codegolf.com/99-bottles-of-beer I see the top 10 entries are all written in

Re: Splitting of string at an interval

2013-04-08 Thread Roy Smith
In article mailman.263.1365390121.3114.python-l...@python.org, Chris Angelico ros...@gmail.com wrote: On Mon, Apr 8, 2013 at 7:48 AM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Like every programming problem, the solution is to break it apart into small, simple steps that

Re: Splitting of string at an interval

2013-04-08 Thread Arnaud Delobelle
On 8 April 2013 14:21, Roy Smith r...@panix.com wrote: For a while, I was rabidly(*) into TDD (Test Driven Development). The cycle I was using was, Write a specification of a behavior, write a (failing) test for that behavior, then write the least possible amount of code to make the test

Re: Splitting of string at an interval

2013-04-08 Thread Roy Smith
On Apr 8, 2013, at 11:10 AM, Arnaud Delobelle wrote: On 8 April 2013 14:21, Roy Smith r...@panix.com wrote: For a while, I was rabidly(*) into TDD (Test Driven Development). The cycle I was using was, Write a specification of a behavior, write a (failing) test for that behavior, then

Re: Splitting of string at an interval

2013-04-08 Thread Chris Angelico
On Tue, Apr 9, 2013 at 1:37 AM, Roy Smith r...@panix.com wrote: I can't help point out, however, that if your initial implementation is to have your code return a constant, it's pretty likely to be an optimum solution in both time and space :-) Likely, but not certain. # 1 def

Re: Splitting of string at an interval

2013-04-08 Thread Arnaud Delobelle
On 8 April 2013 17:20, Chris Angelico ros...@gmail.com wrote: On Tue, Apr 9, 2013 at 1:37 AM, Roy Smith r...@panix.com wrote: I can't help point out, however, that if your initial implementation is to have your code return a constant, it's pretty likely to be an optimum solution in both time

Re: Splitting of string at an interval

2013-04-08 Thread Roy Smith
In article mailman.295.1365438635.3114.python-l...@python.org, Arnaud Delobelle arno...@gmail.com wrote: On 8 April 2013 17:20, Chris Angelico ros...@gmail.com wrote: On Tue, Apr 9, 2013 at 1:37 AM, Roy Smith r...@panix.com wrote: I can't help point out, however, that if your initial

Re: Splitting of string at an interval

2013-04-08 Thread Tim Chase
On 2013-04-08 21:09, Roy Smith wrote: http://codegolf.com/99-bottles-of-beer I see the top 10 entries are all written in Perl. I suppose this says something. About the capabilities of Perl for writing such code, or about the drinking habits of Perl programmers? :-)

Re: Splitting of string at an interval

2013-04-08 Thread Steven D'Aprano
On Mon, 08 Apr 2013 21:09:08 -0400, Roy Smith wrote: There's a whole competition about writing the smallest program which outputs the song 99 bottles of beer: http://codegolf.com/99-bottles-of-beer I see the top 10 entries are all written in Perl. I suppose this says something. When I

Re: Splitting of string at an interval

2013-04-08 Thread Andrew Berg
On 2013.04.08 21:38, Steven D'Aprano wrote: In fact, I may make it a bare . so that not only will it be the shortest program, but also the smallest program in terms of number of non-white pixels. Until someone implements it in Whitespace. -- CPython 3.3.0 | Windows NT 6.2.9200 / FreeBSD 9.1

Re: Splitting of string at an interval

2013-04-08 Thread Chris Angelico
On Tue, Apr 9, 2013 at 12:38 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Mon, 08 Apr 2013 21:09:08 -0400, Roy Smith wrote: There's a whole competition about writing the smallest program which outputs the song 99 bottles of beer: http://codegolf.com/99-bottles-of-beer

Splitting of string at an interval

2013-04-07 Thread subhabangalore
Dear Group, I was looking to split a string in a particular interval, like, If I have a string, string=The Sun rises in the east of our earth I like to see it as, words=[The Sun,rises in,in the,east of,our earth] If any one of the learned members can kindly suggest. Regards, Subhabrata.

Re: Splitting of string at an interval

2013-04-07 Thread Dave Angel
On 04/07/2013 04:25 PM, subhabangal...@gmail.com wrote: Dear Group, I was looking to split a string in a particular interval, like, If I have a string, string=The Sun rises in the east of our earth Are you asserting that there is nothing but letters and whitespace in the string, and that

Re: Splitting of string at an interval

2013-04-07 Thread Steven D'Aprano
On Sun, 07 Apr 2013 13:25:57 -0700, subhabangalore wrote: Dear Group, I was looking to split a string in a particular interval, like, If I have a string, string=The Sun rises in the east of our earth I like to see it as, words=[The Sun,rises in,in the,east of,our earth] If any one

Re: Splitting of string at an interval

2013-04-07 Thread Chris Angelico
On Mon, Apr 8, 2013 at 7:48 AM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Like every programming problem, the solution is to break it apart into small, simple steps that even a computer can follow. ... 5) Shortcut the whole thing, since the problem was underspecified, by

Re: Splitting a string

2010-04-04 Thread Steven D'Aprano
On Sat, 03 Apr 2010 20:10:20 -0700, Patrick Maupin wrote: On Apr 3, 10:00 pm, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: Tests which you know can't fail are called assertions, pre-conditions and post-conditions. We test them because if we don't, they will fail :) Well,

Re: Splitting a string

2010-04-04 Thread Peter Otten
Steven D'Aprano wrote: On Sat, 03 Apr 2010 11:17:36 +0200, Peter Otten wrote: That's certainly faster than a list comprehension (at least on long lists), but it might be a little obscure why the if not s: is needed, The function is small; with a test suite covering the corner cases and

Re: Splitting a string

2010-04-04 Thread Patrick Maupin
On Apr 4, 4:58 am, Peter Otten __pete...@web.de wrote: Personally, though, I prefer unit tests over assertions. IMO, the primary use cases for assertions and unit tests are not the same. When I have a well-defined, clearly understood specification that I am coding to and fully implementing

Re: Splitting a string

2010-04-04 Thread Patrick Maupin
On Apr 4, 2:37 am, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: In any case, the *right* test would be: a = 1 assert a == 1 and a*5==5 and str(a)=='1' and [None,a,None][a] is a You're right. I was very tired when I wrote that, and forgot the last 3 assertions... --

Re: Splitting a string

2010-04-03 Thread Peter Otten
Patrick Maupin wrote: On Apr 2, 4:32 pm, Peter Otten __pete...@web.de wrote: _split = re.compile(r(\d+)).split def split(s): if not s: return () parts = _split(s) parts[1::2] = map(int, parts[1::2]) # because s is non-empty parts contains at least one #

Re: Splitting a string

2010-04-03 Thread Patrick Maupin
On Apr 3, 4:17 am, Peter Otten __pete...@web.de wrote: Patrick Maupin wrote: On Apr 2, 4:32 pm, Peter Otten __pete...@web.de wrote: _split = re.compile(r(\d+)).split def split(s):     if not s:         return ()     parts = _split(s)     parts[1::2] = map(int, parts[1::2])        

Re: Splitting a string

2010-04-03 Thread Steven D'Aprano
On Sat, 03 Apr 2010 11:17:36 +0200, Peter Otten wrote: That's certainly faster than a list comprehension (at least on long lists), but it might be a little obscure why the if not s: is needed, The function is small; with a test suite covering the corner cases and perhaps a comment* nothing

Re: Splitting a string

2010-04-03 Thread Alf P. Steinbach
* Steven D'Aprano: Tests which you know can't fail are called assertions, pre-conditions and post-conditions. We test them because if we don't, they will fail :) :-) It's the umbrella law. Cheers, - Alf -- http://mail.python.org/mailman/listinfo/python-list

Re: Splitting a string

2010-04-03 Thread Patrick Maupin
On Apr 3, 10:00 pm, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: Tests which you know can't fail are called assertions, pre-conditions and post-conditions. We test them because if we don't, they will fail :) Well, yes, but that can get rather tedious at times: a = 1 assert 0 a

Splitting a string

2010-04-02 Thread Thomas Heller
Maybe I'm just lazy, but what is the fastest way to convert a string into a tuple containing character sequences and integer numbers, like this: 'si_pos_99_rep_1_0.ita' - ('si_pos_', 99, '_rep_', 1, '_', 0, '.ita') Thanks for ideas, Thomas --

Re: Splitting a string

2010-04-02 Thread Shashwat Anand
s = 'si_pos_99_rep_1_0.ita' res = tuple(re.split(r'(\d+)', s)) res ('si_pos_', '99', '_rep_', '1', '_', '0', '.ita') On Fri, Apr 2, 2010 at 3:42 PM, Thomas Heller thel...@ctypes.org wrote: Maybe I'm just lazy, but what is the fastest way to convert a string into a tuple containing

Re: Splitting a string

2010-04-02 Thread Xavier Ho
Best I can come up with: def split_number(string): ... output = [string[0]] ... for character in string[1:]: ... if character.isdigit() != output[-1].isdigit(): ... output.append('') ... output[-1] += character ... return tuple(output) ...

Re: Splitting a string

2010-04-02 Thread Xavier Ho
Oops, minor update: def split_number(string): ... output = [string[0]] ... for character in string[1:]: ... if character.isdigit() != output[-1].isdigit(): ... if output[-1].isdigit() is True: ... output[-1] = int(output[-1]) ...

Re: Splitting a string

2010-04-02 Thread Alex Willmer
On Apr 2, 11:12 am, Thomas Heller thel...@ctypes.org wrote: Maybe I'm just lazy, but what is the fastest way to convert a string into a tuple containing character sequences and integer numbers, like this: 'si_pos_99_rep_1_0.ita'  - ('si_pos_', 99, '_rep_', 1, '_', 0, '.ita') This is very

Re: Splitting a string

2010-04-02 Thread Peter Otten
Thomas Heller wrote: Maybe I'm just lazy, but what is the fastest way to convert a string into a tuple containing character sequences and integer numbers, like this: 'si_pos_99_rep_1_0.ita' - ('si_pos_', 99, '_rep_', 1, '_', 0, '.ita') parts =

Re: Splitting a string

2010-04-02 Thread Patrick Maupin
On Apr 2, 6:24 am, Peter Otten __pete...@web.de wrote: Thomas Heller wrote: Maybe I'm just lazy, but what is the fastest way to convert a string into a tuple containing character sequences and integer numbers, like this: 'si_pos_99_rep_1_0.ita'  - ('si_pos_', 99, '_rep_', 1, '_', 0,

Re: Splitting a string

2010-04-02 Thread Terry Reedy
On 4/2/2010 6:21 AM, Shashwat Anand wrote: s = 'si_pos_99_rep_1_0.ita' res = tuple(re.split(r'(\d+)', s)) res ('si_pos_', '99', '_rep_', '1', '_', '0', '.ita') This solves the core of the problem, but is not quite there ;-). Thomas requested conversion of int literals to ints, which is

Re: Splitting a string

2010-04-02 Thread Bearophile
I don't know how fast this is (Python 2.x): from itertools import groupby t = 'si_pos_99_rep_1_0.ita' tuple(int(.join(g)) if h else .join(g) for h,g in groupby(t, str.isdigit)) ('si_pos_', 99, '_rep_', 1, '_', 0, '.ita') It doesn't work with unicode strings. Bye, bearophile --

Re: Splitting a string

2010-04-02 Thread Thomas Heller
Patrick Maupin schrieb: On Apr 2, 6:24 am, Peter Otten __pete...@web.de wrote: Thomas Heller wrote: Maybe I'm just lazy, but what is the fastest way to convert a string into a tuple containing character sequences and integer numbers, like this: 'si_pos_99_rep_1_0.ita' - ('si_pos_', 99,

Re: Splitting a string

2010-04-02 Thread Peter Otten
Thomas Heller wrote: Thanks to all for these code snippets. Peter's solution is the winner - most elegant and also the fastest. With an additional list comprehension to remove the possible empty strings at the start and at the end I get 16 us. Interesting is that Xavier's solution (which

Re: Splitting a string

2010-04-02 Thread Patrick Maupin
On Apr 2, 4:32 pm, Peter Otten __pete...@web.de wrote: _split = re.compile(r(\d+)).split def split(s):     if not s:         return ()     parts = _split(s)     parts[1::2] = map(int, parts[1::2])     if parts[-1] == :         del parts[-1]     if parts[0] == :         del parts[0]    

Re: Splitting a string into substrings of equal size

2009-08-17 Thread Gregor Lingl
Simon Forman schrieb: On Aug 14, 8:22 pm, candide cand...@free.invalid wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a

Re: Splitting a string into substrings of equal size

2009-08-16 Thread Gregor Lingl
Mark Tolonen schrieb: Gregor Lingl gregor.li...@aon.at wrote in message news:4a87036a$0$2292$91cee...@newsreader02.highway.telekom.at... Emile van Sebille schrieb: On 8/14/2009 5:22 PM candide said... ... What is the pythonic way to do this ? I like list comps... jj =

Re: Splitting a string into substrings of equal size

2009-08-16 Thread Simon Forman
On Aug 14, 8:22 pm, candide cand...@free.invalid wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal string with

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Rascal
I'm bored for posting this, but here it is: def add_commas(str): str_list = list(str) str_len = len(str) for i in range(3, str_len, 3): str_list.insert(str_len - i, ',') return ''.join(str_list) -- http://mail.python.org/mailman/listinfo/python-list

Re: Splitting a string into substrings of equal size

2009-08-15 Thread candide
Thanks to all for your response. I particularly appreciate Rascal's solution. -- http://mail.python.org/mailman/listinfo/python-list

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Jan Kaliszewski
Dnia 15-08-2009 o 08:08:14 Rascal jonras...@gmail.com wrote: I'm bored for posting this, but here it is: def add_commas(str): str_list = list(str) str_len = len(str) for i in range(3, str_len, 3): str_list.insert(str_len - i, ',') return ''.join(str_list) For short

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Emile van Sebille
On 8/14/2009 5:22 PM candide said... Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal string with thousands separator.

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Gregor Lingl
What is the pythonic way to do this ? For my part, i reach to this rather complicated code: # -- def comaSep(z,k=3, sep=','): z=z[::-1] x=[z[k*i:k*(i+1)][::-1] for i in range(1+(len(z)-1)/k)][::-1] return sep.join(x) # Test for z in [75096042068045, 509,

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Gregor Lingl
What is the pythonic way to do this ? For my part, i reach to this rather complicated code: # -- def comaSep(z,k=3, sep=','): z=z[::-1] x=[z[k*i:k*(i+1)][::-1] for i in range(1+(len(z)-1)/k)][::-1] return sep.join(x) # Test for z in [75096042068045, 509,

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Gregor Lingl
Emile van Sebille schrieb: On 8/14/2009 5:22 PM candide said... ... What is the pythonic way to do this ? I like list comps... jj = '1234567890123456789' ,.join([jj[ii:ii+3] for ii in range(0,len(jj),3)]) '123,456,789,012,345,678,9' Emile Less beautiful but more correct:

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Mark Tolonen
Gregor Lingl gregor.li...@aon.at wrote in message news:4a87036a$0$2292$91cee...@newsreader02.highway.telekom.at... Emile van Sebille schrieb: On 8/14/2009 5:22 PM candide said... ... What is the pythonic way to do this ? I like list comps... jj = '1234567890123456789'

Re: Splitting a string into substrings of equal size

2009-08-15 Thread ryles
On Aug 14, 8:22 pm, candide cand...@free.invalid wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal string with

Re: Splitting a string into substrings of equal size

2009-08-15 Thread MRAB
ryles wrote: On Aug 14, 8:22 pm, candide cand...@free.invalid wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Brian
On Sat, Aug 15, 2009 at 4:06 PM, MRAB pyt...@mrabarnett.plus.com wrote: ryles wrote: On Aug 14, 8:22 pm, candide cand...@free.invalid wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at

Re: Splitting a string into substrings of equal size

2009-08-15 Thread MRAB
Brian wrote: On Sat, Aug 15, 2009 at 4:06 PM, MRAB pyt...@mrabarnett.plus.com mailto:pyt...@mrabarnett.plus.com wrote: ryles wrote: On Aug 14, 8:22 pm, candide cand...@free.invalid wrote: Suppose you need to split a string into substrings of a given

Re: Splitting a string into substrings of equal size

2009-08-15 Thread ryles
On Aug 15, 6:28 pm, MRAB pyt...@mrabarnett.plus.com wrote:       for z in [75096042068045, 509, 12024, 7, 2009]:            print re.sub(r(?=.)(?=(?:...)+$), ,, z)     75,096,042,068,045     509     12,024     7     2,009 The call replaces a zero-width match with a comma, ie

Re: Splitting a string into substrings of equal size

2009-08-14 Thread Gabriel Genellina
En Fri, 14 Aug 2009 21:22:57 -0300, candide cand...@free.invalid escribió: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by

Re: Splitting a string into substrings of equal size

2009-08-14 Thread Jan Kaliszewski
15-08-2009 candide cand...@free.invalid wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal string with

Re: Splitting a string into substrings of equal size

2009-08-14 Thread Jan Kaliszewski
15-08-2009 Jan Kaliszewski z...@chopin.edu.pl wrote: 15-08-2009 candide cand...@free.invalid wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example

splitting a string into an array using a time value

2008-10-14 Thread Joe Python
I want to find a way to split a string into an array using a time value. s = r 8/25/2008 11:10:08 AM Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed imperdiet luctus nisl. ipsum vel arcu gravida mattis. In mattis dolor id sem. Praesent dictum tortor non lacus. 0/3/2008

Re: splitting a string into an array using a time value

2008-10-14 Thread Gabriel Genellina
En Tue, 14 Oct 2008 18:08:53 -0300, Joe Python [EMAIL PROTECTED] escribió: I want to find a way to split a string into an array using a time value. s = r 8/25/2008 11:10:08 AM Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed imperdiet luctus nisl. ipsum vel arcu

Splitting a string

2007-05-15 Thread HMS Surprise
The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my split that works with shorter strings does not seem to work with this one. Ideas? Thanks, jvh s = D132258\',\'\',

Re: Splitting a string

2007-05-15 Thread Nick Vatamaniuc
On May 15, 2:28 pm, HMS Surprise [EMAIL PROTECTED] wrote: The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my split that works with shorter strings does not seem to work with

Re: Splitting a string

2007-05-15 Thread HMS Surprise
I found my problem, the backslash isn't really there. It is just the way it was displayed in the shell after being split from a larger string. Printing it yields D132259','','status=no,location=no,width=630,height=550,left=200,top=100') target=_blank class=dvLink title=Send an Email to selected

Re: Splitting a string

2007-05-15 Thread Duncan Booth
HMS Surprise [EMAIL PROTECTED] wrote: The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my split that works with shorter strings does not seem to work with this one. Ideas?

Re: Splitting a string

2007-05-15 Thread Gary Herron
HMS Surprise wrote: The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my split that works with shorter strings does not seem to work with this one. Ideas? Thanks, jvh s =

Re: Splitting a string

2007-05-15 Thread HMS Surprise
On May 15, 2:04 pm, Nick Vatamaniuc [EMAIL PROTECTED] wrote: On May 15, 2:28 pm, HMS Surprise [EMAIL PROTECTED] wrote: The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my

Re: Splitting a string

2007-05-15 Thread HMS Surprise
Thanks everyone. The shell's display really threw me off. Don't really understand why it looks different typing t vs print t. Now that I can see past that split works just as advertised. Not real clear on triple quotes but I have seen it used and I can see where triple is needed to differentiate

Re: Splitting a string with extra parameters

2006-04-06 Thread Fulvio
Alle 11:23, giovedì 06 aprile 2006, Chris P ha scritto: when splitting based on a delimiter of , the above string gets broken up in 5 columns instead of 4 due to the , in the money amount. There should be cvs package in the python directory. Why don't you try that way? Reading some help gives

Re: Splitting a string with extra parameters

2006-04-06 Thread Amit Khemka
I guess you should use re module ... In this case re.split(\D,\D, YOUR_STRING) should work. (splits only when , is between two non-digits). for details and more options see python-docs. cheers, amit. On 4/6/06, Fulvio [EMAIL PROTECTED] wrote: Alle 11:23, giovedì 06 aprile 2006, Chris P ha

Re: Splitting a string with extra parameters

2006-04-06 Thread Andrew Gwozdziewycz
On Apr 6, 2006, at 7:38 AM, Amit Khemka wrote: I guess you should use re module ... In this case re.split(\D,\D, YOUR_STRING) should work. (splits only when , is between two non-digits). This works assuming all line elements are quoted. This would fail if (and this too my knowledge is

Splitting a string with extra parameters

2006-04-05 Thread Chris P
Hello list, I just started using python and I must say I enjoy it very much. I do have an issue in which I hope to get some pointers to. I have a string, which I need to split based on a delimiter. This I know how to do. But what I cannot figure out is, take for example the following: column 1

Re: Splitting a string

2006-02-14 Thread Fredrik Lundh
Nico Grubert wrote: I'd like to split a string where 'and', 'or', 'and not' occurs. Example string: s = 'Smith, R. OR White OR Blue, T. AND Black AND Red AND NOT Green' I need to split s in order to get this list: ['Smith, R.', 'White', 'Blue, T.', 'Back', 'Red', 'Green'] Any idea, how I

Re: Splitting a string

2006-02-14 Thread Dylan Moreland
Take a look at: http://docs.python.org/lib/node115.html#l2h-878 So I would try something like: pat = re.compile(r (?:AND|OR|AND NOT) ) pat.split(string) Compile the regular expression with re.IGNORECASE if you like. Nico Grubert wrote: Dear Python users, I'd like to split a string where

Re: Splitting a string

2006-02-14 Thread Xie Yanbo
On 2/14/06, Nico Grubert [EMAIL PROTECTED] wrote: Dear Python users, I'd like to split a string where 'and', 'or', 'and not' occurs. Example string: s = 'Smith, R. OR White OR Blue, T. AND Black AND Red AND NOT Green' I need to split s in order to get this list: ['Smith, R.', 'White',

Re: Splitting a string

2006-02-14 Thread Fredrik Lundh
Dylan Moreland wrote: So I would try something like: pat = re.compile(r (?:AND|OR|AND NOT) ) pat.split(string) footnote: this yields: ['Smith, R.', 'White', 'Blue, T.', 'Black', 'Red', 'NOT Green'] (the | operator picks the first (leftmost) alternative that results in an overall

Re: Splitting a string

2006-02-14 Thread Dylan Moreland
Woops! Thanks for the correction. I was assuming greediness for some reason. Fredrik Lundh wrote: Dylan Moreland wrote: So I would try something like: pat = re.compile(r (?:AND|OR|AND NOT) ) pat.split(string) footnote: this yields: ['Smith, R.', 'White', 'Blue, T.', 'Black',

Re: Splitting a string

2006-02-14 Thread Peter Otten
Fredrik Lundh wrote: re.split((?i)\s*(?:and not|and|or)\s*, s) ['Smith, R.', 'White', 'Blue, T.', 'Black', 'Red', 'Green'] This fails for people with nasty names: s = James White or Andy Grove and Jack Orr and not James Grand re.split((?i)\s*(?:and not|and|or)\s*, s) ['James White', '', 'y

Re: Splitting a string

2006-02-14 Thread Paul Rubin
Nico Grubert [EMAIL PROTECTED] writes: I'd like to split a string where 'and', 'or', 'and not' occurs. Other people have suggested how to do this splitting. But don't you really want a parser? -- http://mail.python.org/mailman/listinfo/python-list

Re: Splitting a string

2006-02-14 Thread Christoph Zwerschke
Nico Grubert wrote: I'd like to split a string where 'and', 'or', 'and not' occurs. Example string: s = 'Smith, R. OR White OR Blue, T. AND Black AND Red AND NOT Green' Here is a solution without using the re module: s.replace(' AND NOT ', ' OR ').replace(' AND ', ' OR ').split(' OR ') --

Splitting a string

2006-02-13 Thread Nico Grubert
Dear Python users, I'd like to split a string where 'and', 'or', 'and not' occurs. Example string: s = 'Smith, R. OR White OR Blue, T. AND Black AND Red AND NOT Green' I need to split s in order to get this list: ['Smith, R.', 'White', 'Blue, T.', 'Back', 'Red', 'Green'] Any idea, how I can

Re: newby question: Splitting a string - separator

2005-12-10 Thread Tim Roberts
James Stroud [EMAIL PROTECTED] wrote: The one I like best goes like this: py data = Guido van Rossum Tim Peters Thomas Liesner py names = [n for n in data.split() if n] py names ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner'] I think it is theoretically faster (and more

Re: newby question: Splitting a string - separator

2005-12-10 Thread Fredrik Lundh
James Stroud wrote: py data = Guido van Rossum Tim Peters Thomas Liesner py names = [n for n in data.split() if n] py names ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner'] I think it is theoretically faster (and more pythonic) than using regexes.

Re: newby question: Splitting a string - separator

2005-12-09 Thread James Stroud
Thomas Liesner wrote: Hi all, i am having a textfile which contains a single string with names. I want to split this string into its records an put them into a list. In normal cases i would do something like: #!/usr/bin/python inp = open(file) data = inp.read() names = data.split()

Re: newby question: Splitting a string - separator

2005-12-09 Thread Kent Johnson
James Stroud wrote: The one I like best goes like this: py data = Guido van Rossum Tim Peters Thomas Liesner py names = [n for n in data.split() if n] py names ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner'] I think it is theoretically faster (and more pythonic)

Re: newby question: Splitting a string - separator

2005-12-09 Thread Tim Peters
[James Stroud] The one I like best goes like this: py data = Guido van Rossum Tim Peters Thomas Liesner py names = [n for n in data.split() if n] py names ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner'] I think it is theoretically faster (and more pythonic) than using

Re: newby question: Splitting a string - separator

2005-12-09 Thread bonono
Thomas Liesner wrote: Hi all, i am having a textfile which contains a single string with names. I want to split this string into its records an put them into a list. In normal cases i would do something like: #!/usr/bin/python inp = open(file) data = inp.read() names = data.split()

Re: newby question: Splitting a string - separator

2005-12-09 Thread James Stroud
Kent Johnson wrote: James Stroud wrote: The one I like best goes like this: py data = Guido van Rossum Tim Peters Thomas Liesner py names = [n for n in data.split() if n] py names ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner'] I think it is theoretically faster

Re: newby question: Splitting a string - separator

2005-12-09 Thread Michael Spencer
[EMAIL PROTECTED] wrote: Thomas Liesner wrote: Hi all, i am having a textfile which contains a single string with names. I want to split this string into its records an put them into a list. In normal cases i would do something like: #!/usr/bin/python inp = open(file) data = inp.read()

Re: newby question: Splitting a string - separator

2005-12-09 Thread Steven D'Aprano
On Fri, 09 Dec 2005 18:02:02 -0800, James Stroud wrote: Thomas Liesner wrote: Hi all, i am having a textfile which contains a single string with names. I want to split this string into its records an put them into a list. In normal cases i would do something like: #!/usr/bin/python inp

Re: newby question: Splitting a string - separator

2005-12-09 Thread James Stroud
Steven D'Aprano wrote: On Fri, 09 Dec 2005 18:02:02 -0800, James Stroud wrote: Thomas Liesner wrote: Hi all, i am having a textfile which contains a single string with names. I want to split this string into its records an put them into a list. In normal cases i would do something like:

newby question: Splitting a string - separator

2005-12-08 Thread Thomas Liesner
Hi all, i am having a textfile which contains a single string with names. I want to split this string into its records an put them into a list. In normal cases i would do something like: #!/usr/bin/python inp = open(file) data = inp.read() names = data.split() inp.close() The problem is,

Re: newby question: Splitting a string - separator

2005-12-08 Thread Michael Spencer
Thomas Liesner wrote: Hi all, i am having a textfile which contains a single string with names. I want to split this string into its records an put them into a list. In normal cases i would do something like: #!/usr/bin/python inp = open(file) data = inp.read() names = data.split()

Re: newby question: Splitting a string - separator

2005-12-08 Thread Noah
Thomas Liesner wrote: ... The only thing i can rely on, ist that the recordseparator is always more than a single whitespace. I thought of something like defining the separator for split() by using a regex for more than one whitespace. RegEx for whitespace is \s, but what would i use for

Re: newby question: Splitting a string - separator

2005-12-08 Thread Jim
Hi Tom, a regex for more than one whitespace. RegEx for whitespace is \s, but what would i use for more than one? \s+? For more than one, I'd use \s\s+ -Jim -- http://mail.python.org/mailman/listinfo/python-list

Re: Problem splitting a string

2005-10-15 Thread Mike Meyer
Robert Kern [EMAIL PROTECTED] writes: Anthony Liu wrote: I have this simple string: mystr = 'this_NP is_VL funny_JJ' I want to split it and give me a list as ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] You could use regular expressions as Jason Stitt mentions, or you could replace '_'

Re: Problem splitting a string

2005-10-15 Thread Steven D'Aprano
On Fri, 14 Oct 2005 21:52:07 -0700, Anthony Liu wrote: I have this simple string: mystr = 'this_NP is_VL funny_JJ' I want to split it and give me a list as ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] I think the documentation does say that the separator/delimiter can be a string

Re: Problem splitting a string

2005-10-15 Thread Paul Rubin
Anthony Liu [EMAIL PROTECTED] writes: I do I split the string by using both ' ' and '_' as the delimiters at once? Use re.split. -- http://mail.python.org/mailman/listinfo/python-list

Re: Problem splitting a string

2005-10-15 Thread Alex Martelli
Steven D'Aprano [EMAIL PROTECTED] wrote: ... You can *almost* do that as a one-liner: No 'almost' about it... L2 = [item.split('_') for item in mystr.split()] except that gives a list like this: [['this', 'NP'], ['is', 'VL'], ['funny', 'JJ']] which needs flattening. because

Re: Problem splitting a string

2005-10-15 Thread Alex Martelli
Mike Meyer [EMAIL PROTECTED] wrote: ... A third alternative is to split once, then split the substrings a second time and stitch the results back together: sum([x.split('_') for x in mystr.split()], []) ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] Which is probably slow. To bad extend

Re: Problem splitting a string

2005-10-15 Thread Steven D'Aprano
On Sat, 15 Oct 2005 10:51:41 +0200, Alex Martelli wrote: Steven D'Aprano [EMAIL PROTECTED] wrote: ... You can *almost* do that as a one-liner: No 'almost' about it... L2 = [item.split('_') for item in mystr.split()] except that gives a list like this: [['this', 'NP'], ['is',

Re: Problem splitting a string

2005-10-15 Thread SPE - Stani's Python Editor
Use re.split, as this is the fastest and cleanest way. However, iff you have to split a lot of strings, the best is: import re delimiters = re.compile('_| ') def split(x): return delimiters.split(x) split('this_NP is_VL funny_JJ') ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] Stani -- SPE -

Re: Problem splitting a string

2005-10-15 Thread Fredrik Lundh
SPE - Stani's Python Editor wrote: Use re.split, as this is the fastest and cleanest way. However, iff you have to split a lot of strings, the best is: import re delimiters = re.compile('_| ') def split(x): return delimiters.split(x) or, shorter: import re split =

Re: Problem splitting a string

2005-10-15 Thread Kent Johnson
Alex Martelli wrote: Using sum on lists is DEFINITELY slow -- avoid it like the plague. If you have a list of lists LOL, DON'T use sum(LOL, []), but rather [x for x in y for y in LOL] Should be lol = [[1,2],[3,4]] [x for y in lol for x in y] [1, 2, 3, 4] The outer loop comes first.

  1   2   >