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
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
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
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
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
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
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
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? :-)
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
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
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
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.
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
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
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
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,
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
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
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...
--
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
#
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])
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
* 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
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
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
--
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
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)
...
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])
...
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
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 =
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,
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
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
--
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,
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
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]
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
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 =
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
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
Thanks to all for your response. I particularly appreciate Rascal's solution.
--
http://mail.python.org/mailman/listinfo/python-list
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
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.
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,
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,
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:
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'
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
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
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
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
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
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
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
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
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
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
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\',\'\',
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
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
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?
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 =
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
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
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
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
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
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
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
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
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',
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
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',
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
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
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 ')
--
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
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
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.
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()
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)
[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
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()
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
[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()
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
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:
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,
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()
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
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
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 '_'
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
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
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
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
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',
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 -
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 =
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 - 100 of 120 matches
Mail list logo