Re: Splitting a string

2010-04-04 Thread Patrick Maupin
On Apr 4, 2:37 am, Steven D'Aprano 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... -- http://mail.python.org/mailman/listinfo/py

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 w

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

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 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 th

Re: Splitting a string

2010-04-03 Thread Patrick Maupin
On Apr 3, 10:00 pm, Steven D'Aprano 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 < 2 b = a + 3 assert 2 < b - a < 4

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 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*

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

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 l

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] == "": >        

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 (whic

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'

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 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, wh

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,

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.compile("([+-]?\d+)").split('s

Re: Splitting a string

2010-04-02 Thread Alex Willmer
On Apr 2, 11:12 am, 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') > This is very probably not the

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]) ... output.a

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) ... >>> split_numb

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 wrote: > Maybe I'm just lazy, but what is the fastest way to convert a string > into a tuple containing character se

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 -- http://mail.python.org/mailman/listinfo/python-li

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 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-16 Thread Simon Forman
On Aug 14, 8:22 pm, candide 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 thousands > separ

Re: Splitting a string into substrings of equal size

2009-08-16 Thread Gregor Lingl
Mark Tolonen schrieb: "Gregor Lingl" 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 15, 6:28 pm, MRAB 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,

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 > wrote: ryles wrote: On Aug 14, 8:22 pm, candide wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substr

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Brian
On Sat, Aug 15, 2009 at 4:06 PM, MRAB wrote: > ryles wrote: > >> On Aug 14, 8:22 pm, candide 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. >

Re: Splitting a string into substrings of equal size

2009-08-15 Thread MRAB
ryles wrote: On Aug 14, 8:22 pm, candide 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 thousands

Re: Splitting a string into substrings of equal size

2009-08-15 Thread ryles
On Aug 14, 8:22 pm, candide 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 thousands > separ

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Mark Tolonen
"Gregor Lingl" 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' >>> ",".join([jj[ii:ii+3] for i

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 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 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 Jan Kaliszewski
Dnia 15-08-2009 o 08:08:14 Rascal 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 strings (for sure most

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-14 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-14 Thread Jan Kaliszewski
15-08-2009 Jan Kaliszewski wrote: 15-08-2009 candide 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 str

Re: Splitting a string into substrings of equal size

2009-08-14 Thread Jan Kaliszewski
15-08-2009 candide 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 thousands separator. I'd

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 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 formatting a decimal string wi

Splitting a string into substrings of equal size

2009-08-14 Thread candide
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. What is the pythonic way to do this ?

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 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/200

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 fr

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 fir

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, > jv

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. > > I

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 select

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 wi

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\',\'\', \'status=

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 knowledg

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

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

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 Nico Grubert
> re.split("(?i)\s*(and not|and|or)\s*", s) Thanks, Frederik for the step by step howto! And also thanks to you, Dylan. re.split("(?i)\s*(and not|and|or)\s*", s) is almost right. I changed it to: words = re.split("(?i)\s*( and not | and | or )\s*", s) in order to handle words containing "or"

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 ') --

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 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) ['Jame

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.

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 m

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.', 'W

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 wher

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 ide

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 spl

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 > >

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 fast

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

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: >> >> >>>#!

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"

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 t

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() > > name

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

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 p

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 =

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: 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

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 =

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 probl

Re: Problem splitting a string

2005-10-15 Thread Kent Johnson
Steven D'Aprano wrote: > On Sat, 15 Oct 2005 10:51:41 +0200, Alex Martelli wrote: >>[ x for x in y.split('_') for y in z.split(' ') ] > > py> mystr = 'this_NP is_VL funny_JJ' > py> [x for x in y.split('_') for y in mystr.split(' ')] > Traceback (most recent call last): > File "", line 1, in ? >

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

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.

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 - S

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',

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 ba

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.

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 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 strin

Re: Problem splitting a string

2005-10-14 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 cou

Re: Problem splitting a string

2005-10-14 Thread Robert Kern
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 representing all > delimiters we wa

Re: Problem splitting a string

2005-10-14 Thread Erik Max Francis
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'] > > 1. I tried mystr.split('_| '), but this gave me: > > ['this_NP is_VL funny_JJ'] > > It is not splitted at all.

Re: Problem splitting a string

2005-10-14 Thread Jason Stitt
On Oct 14, 2005, at 11:52 PM, 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']1. I tried mystr.split('_| '), but this gave me:['this_NP is_VL funny_JJ']Try re.split, as in:import rere.split(

Problem splitting a string

2005-10-14 Thread Anthony Liu
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'] 1. I tried mystr.split('_| '), but this gave me: ['this_NP is_VL funny_JJ'] It is not splitted at all. 2. I tried mystr.split('_'), and this gave me:

Re: Splitting a string into groups of three characters

2005-08-08 Thread John Machin
William Park wrote: > [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > >>Hi, >> >>Is there a function that split a string into groups, containing an "x" >>amount of characters? >> >>Ex. >>TheFunction("Hello World",3) >> >>Returns: >> >>['Hell','o W','orl','d'] >> >> >>Any reply would be truly apprec

Re: Splitting a string into groups of three characters

2005-08-08 Thread Gregory Piñero
I guess this question has already been thouroughly answered but here is my version: def split(string,n): outlist=[] for bottom in range(0,len(string),n): top=min(bottom+n,len(string)) outlist.append(string[bottom:top]) return outlist On 8/8/05, William Park <[EMAIL

Re: Splitting a string into groups of three characters

2005-08-08 Thread William Park
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, > > Is there a function that split a string into groups, containing an "x" > amount of characters? > > Ex. > TheFunction("Hello World",3) > > Returns: > > ['Hell','o W','orl','d'] > > > Any reply would be truly appreciated. Look into 're' mo

Re: Splitting a string into groups of three characters

2005-08-08 Thread Cyril Bazin
Another solution derived from an old discussion about the same problem? def takeBy(s, n):     import itertools     list(''.join(x) for x in itertools.izip(*[iter(s)]*n)) (Hoping len(s) % n = 0) CyrilOn 8 Aug 2005 11:04:31 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED] > wrote:Yes i know i made a mi

Re: Splitting a string into groups of three characters

2005-08-08 Thread [EMAIL PROTECTED]
Yes i know i made a mistake, >['Hell','o W','orl','d'] but you know what I mean lol, I'll probly use John Machin's def nsplit(s, n): return [s[k:k+n] for k in xrange(0, len(s), n)] It seems fast, and does not require any imports. But anyways, thank you for all your help, you rock! :) --

Re: Splitting a string into groups of three characters

2005-08-08 Thread [EMAIL PROTECTED]
Yes i know i made a mistake, >['Hell','o W','orl','d'] but you know what I mean lol, I'll probly use John Machin's def nsplit(s, n): return [s[k:k+n] for k in xrange(0, len(s), n)] It seems fast, and does not require any imports. But anyways, thank you for all your help, you rpck! :) --

Re: Splitting a string into groups of three characters

2005-08-08 Thread Maksim Kasimov
import re str = '123456789983qw' re.findall("(.{3})", str)+[str[-(len(str) % 3):]] [EMAIL PROTECTED] wrote: > Hi, > > Is there a function that split a string into groups, containing an "x" > amount of characters? > > Ex. > TheFunction("Hello World",3) > > Returns: > > ['Hell','o W','orl','d']

Re: Splitting a string into groups of three characters

2005-08-07 Thread John Machin
gene tani wrote: > Um, you shd 1st search cookbook, or Vaults Parnassu, dmoz python > section, pypackage: > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347689 which includes e.g. def each_slice_lol(listin,n): """non-overlapp'g slices, return (list of lists) """ len_listin=le

  1   2   >