Re: Pythonic way to iterate through multidimensional space?

2014-10-08 Thread Gelonida N
On 10/7/2014 1:01 PM, Ned Batchelder wrote: On 10/7/14 2:10 AM, Gelonida N wrote: Disadvantage of itertools.product() is, that it makes a copy in memory. Reason ist, that itertools also makes products of generators (meaning of objects, that one can't iterate several times through) There are t

Re: Pythonic way to iterate through multidimensional space?

2014-10-07 Thread Ned Batchelder
On 10/7/14 2:10 AM, Gelonida N wrote: Disadvantage of itertools.product() is, that it makes a copy in memory. Reason ist, that itertools also makes products of generators (meaning of objects, that one can't iterate several times through) There are two use cases, that I occasionaly stumble over

Re: Pythonic way to iterate through multidimensional space?

2014-10-06 Thread Gelonida N
On 8/6/2014 1:39 PM, Tim Chase wrote: On 2014-08-06 11:04, Gayathri J wrote: Below is the code I tried to check if itertools.product() was faster than normal nested loops... they arent! arent they supposed to be...or am i making a mistake? I believe something like this was discussed a while a

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Gayathri J
Dear Peter thanks . But thats what I was trying to say just taking them to zero by f[:,:,:] = 0.0 or using np.zeros is surely going to give me a time gain... but my example of using the itertools.product() and doing f[x] =0.0 is just to compare the looping timing with the traditional nested loops

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Tim Chase
On 2014-08-06 11:04, Gayathri J wrote: > Below is the code I tried to check if itertools.product() was > faster than normal nested loops... > > they arent! arent they supposed to be...or am i making a mistake? I believe something like this was discussed a while ago and there was a faster-but-ugli

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Peter Otten
Gayathri J wrote: > Dear Peter > > Yes the f[t] or f[:,:,:] might give a marginal increase, The speedup compared itertools.product() is significant: $ python -m timeit -s 'from itertools import product; from numpy.random import rand; N = 100; a = rand(N, N, N); r = range(N)' 'for x in produc

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Gayathri J
Dear Peter Yes the f[t] or f[:,:,:] might give a marginal increase, but then i need to do further operations using the indices, in which case this wouldnt help Dear Wojciech np.flat() works if u dont care about the indices and only the matrix/array values matter. but if the matters, flatten

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Wojciech Giel
You might check numpy it is really powerful tool for working with multi dimensional arrays: ex. >>> a = arange(81).reshape(3,3,3,3) >>> a array( 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 1

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Peter Otten
Gayathri J wrote: > Dear Peter > > Below is the code I tried to check if itertools.product() was faster than > normal nested loops... > > they arent! arent they supposed to be... I wouldn't have expected product() to be significantly faster, but neither did I expect it to be slower. > or am i

Re: Pythonic way to iterate through multidimensional space?

2014-08-06 Thread Mark Lawrence
On 06/08/2014 06:34, Gayathri J wrote: Dear Peter Below is the code I tried to check if itertools.product() was faster than normal nested loops... they arent! arent they supposed to be...or am i making a mistake? any idea? * * * * *# -

Re: Pythonic way to iterate through multidimensional space?

2014-08-05 Thread Chris Angelico
On Wed, Aug 6, 2014 at 3:34 PM, Gayathri J wrote: > Below is the code I tried to check if itertools.product() was faster than > normal nested loops... > > they arent! arent they supposed to be...or am i making a mistake? any idea? Don't worry about what's faster. That almost never matters. Worry,

Re: Pythonic way to iterate through multidimensional space?

2014-08-05 Thread Gayathri J
Dear Peter Below is the code I tried to check if itertools.product() was faster than normal nested loops... they arent! arent they supposed to be...or am i making a mistake? any idea? ** *# -*- coding: utf-8 -*-* *import numpy as np*

Re: Pythonic way to iterate through multidimensional space?

2014-08-05 Thread Frank Miles
On Tue, 05 Aug 2014 20:06:05 +, Frank Miles wrote: > I need to evaluate a complicated function over a multidimensional space > as part of an optimization problem. This is a somewhat general problem > in which the number of dimensions and the function being evaluated can > vary from problem to

Re: Pythonic way to iterate through multidimensional space?

2014-08-05 Thread Peter Otten
Frank Miles wrote: > I need to evaluate a complicated function over a multidimensional space > as part of an optimization problem. This is a somewhat general problem > in which the number of dimensions and the function being evaluated can > vary from problem to problem. > > I've got a working ve

Re: Pythonic way to count sequences

2013-04-25 Thread CM
Thank you, everyone, for the answers. Very helpful and knowledge- expanding. -- http://mail.python.org/mailman/listinfo/python-list

Re: Pythonic way to count sequences

2013-04-25 Thread Matthew Gilson
A Counter is definitely the way to go about this. Just as a little more information. The below example can be simplified: from collections import Counter count = Counter(mylist) With the other example, you could have achieved the same thing (and been backward compatible to python2.

Re: Pythonic way to count sequences

2013-04-25 Thread Modulok
On 4/25/13, Denis McMahon wrote: > On Wed, 24 Apr 2013 22:05:52 -0700, CM wrote: > >> I have to count the number of various two-digit sequences in a list such >> as this: >> >> mylist = [(2,4), (2,4), (3,4), (4,5), (2,1)] # (Here the (2,4) sequence >> appears 2 times.) >> >> and tally up the resu

Re: Pythonic way to count sequences

2013-04-25 Thread Denis McMahon
On Wed, 24 Apr 2013 22:05:52 -0700, CM wrote: > I have to count the number of various two-digit sequences in a list such > as this: > > mylist = [(2,4), (2,4), (3,4), (4,5), (2,1)] # (Here the (2,4) sequence > appears 2 times.) > > and tally up the results, assigning each to a variable. The in

Re: Pythonic way to count sequences

2013-04-25 Thread Serhiy Storchaka
25.04.13 08:26, Chris Angelico написав(ла): So you can count them up directly with a dictionary: count = {} for sequence_tuple in list_of_tuples: count[sequence_tuple] = count.get(sequence_tuple,0) + 1 Or alternatives: count = {} for sequence_tuple in list_of_tuples: if sequence_tup

Re: Pythonic way to count sequences

2013-04-24 Thread Steven D'Aprano
On Wed, 24 Apr 2013 22:05:52 -0700, CM wrote: > I have to count the number of various two-digit sequences in a list such > as this: > > mylist = [(2,4), (2,4), (3,4), (4,5), (2,1)] # (Here the (2,4) sequence > appears 2 times.) > > and tally up the results, assigning each to a variable. The in

Re: Pythonic way to count sequences

2013-04-24 Thread Chris Angelico
On Thu, Apr 25, 2013 at 3:05 PM, CM wrote: > I have to count the number of various two-digit sequences in a list > such as this: > > mylist = [(2,4), (2,4), (3,4), (4,5), (2,1)] # (Here the (2,4) > sequence appears 2 times.) > > and tally up the results, assigning each to a variable. You can use

Re: Pythonic way for retrieving value for a nested dictionary.

2013-03-04 Thread Dave Angel
On 03/05/2013 01:48 AM, Lowly Minion wrote: For a typical dict: i.e. d = { '1': 'a', '2', 'b' } I might use something like: d.get('1', None) To get the value of 1. What would be the most pythonic way of getting a nested value of a dictionary within a list: some_list = [{ 'item': { 'letter'

Re: pythonic way

2012-11-02 Thread Chris Angelico
On Fri, Nov 2, 2012 at 7:58 PM, jack wrote: > thanks,but I don't think enumerate() is my want > Have some ways to operate the reference of element,not a copy when I tried > to traverse a list? > > I'm so sorry about my poor English, hope you don't mind it. No probs, I'll be a little less vague an

Re: pythonic way

2012-11-02 Thread jack
thanks,but I don't think enumerate() is my want Have some ways to operate the reference of element,not a copy when I tried to traverse a list? I'm so sorry about my poor English, hope you don't mind it. On 2012/11/2 15:56, Chris Angelico wrote: On Fri, Nov 2, 2012 at 6:14 PM, jack wrote: So

Re: pythonic way

2012-11-02 Thread Chris Angelico
On Fri, Nov 2, 2012 at 6:14 PM, jack wrote: > Sometimes, I need to alter the element as traverse a list like this (it's a > sample): > c = range(10) > i = 0 > for ele in c: > # do something > # branch: > c[i] = # value > i += 1 > > How to be pythonic

Re: pythonic way

2012-11-02 Thread jack
Sometimes, I need to alter the element as traverse a list like this (it's a sample): c = range(10) i = 0 for ele in c: # do something # branch: c[i] = # value i += 1 How to be pythonic? 2012/11/2 0:54, Zero Piraeus : : On 1 November 2012 11:32,

Re: pythonic way

2012-11-01 Thread inshu chauhan
OK ..I got it.. On Thu, Nov 1, 2012 at 5:54 PM, Zero Piraeus wrote: > : > > On 1 November 2012 11:32, inshu chauhan wrote: > > what is the most pythonic way to do this : > > > >if 0 < ix < 10 and 0 < iy < 10 ??? > > As everyone else has said, it's perfectly pythonic once yo

Re: pythonic way

2012-11-01 Thread Zero Piraeus
: On 1 November 2012 11:32, inshu chauhan wrote: > what is the most pythonic way to do this : > >if 0 < ix < 10 and 0 < iy < 10 ??? As everyone else has said, it's perfectly pythonic once you stick the colon on the end. You might find it more instantly readable with some ext

Re: pythonic way

2012-11-01 Thread Ian Kelly
On Thu, Nov 1, 2012 at 9:32 AM, inshu chauhan wrote: > what is the most pythonic way to do this : > >if 0 < ix < 10 and 0 < iy < 10 ??? > I suppose you could do if all(0 < i < 10 for i in (ix, iy)): but I think that the original is more readable unless you have several

Re: pythonic way

2012-11-01 Thread Terry Reedy
On 11/1/2012 11:32 AM, inshu chauhan wrote: what is the most pythonic way to do this : if 0 < ix < 10 and 0 < iy < 10 ??? end with : instead of ??? >>> ix, iy = 3,4 >>> if 0 < ix < 10 and 0 < iy < 10: print('This was too easy') This was too easy -- Ter

Re: pythonic way

2012-11-01 Thread Tim Chase
On 11/01/12 10:32, inshu chauhan wrote: > what is the most pythonic way to do this : > >if 0 < ix < 10 and 0 < iy < 10 ??? What's wrong with the syntax you provide? It's perfectly pythonic: ix = 42 yx = 3.14159 if 0 < ix < 10 and 0 < iy < 10: do_stuff(ix, iy) el

Re: pythonic way

2012-11-01 Thread MRAB
On 2012-11-01 15:32, inshu chauhan wrote: what is the most pythonic way to do this : if 0 < ix < 10 and 0 < iy < 10 ??? That looks Pythonic to me, except for the missing colon. -- http://mail.python.org/mailman/listinfo/python-list

Re: Pythonic way with more than one max possible

2011-07-20 Thread CM
Thanks, everyone. Very helpful! Che -- http://mail.python.org/mailman/listinfo/python-list

Re: Pythonic way with more than one max possible

2011-07-20 Thread Thomas Jollans
On 20/07/11 06:19, Steven D'Aprano wrote: > On Wed, 20 Jul 2011 01:17 pm CM wrote: > >> I have three items in a dict, like this: >> >> the_dict = {'a':1, 'b':2, 'c':3} >> >> but the vals could be anything. I want to configure something else >> based on the "winner" of such a dict, with these rule

Re: Pythonic way with more than one max possible

2011-07-20 Thread Chris Rebert
On Tue, Jul 19, 2011 at 10:10 PM, CM wrote: > On Jul 19, 11:17 pm, CM wrote: >> I have three items in a dict, like this: >> >> the_dict = {'a':1, 'b':2, 'c':3} >> >> but the vals could be anything.  I want to configure something else >> based on the "winner" of such a dict, with these rules: > I

Re: Pythonic way with more than one max possible

2011-07-19 Thread woooee
> 1. In this dict, if there is a UNIQUE max value, then its *key* is the > winner. > 2. If there are any TIES for max value, then the *key* 'b' is the > winner by default. This will store the max value(s) in a list. In case of a tie, you can take the first value in the list, but it may be differe

Re: Pythonic way with more than one max possible

2011-07-19 Thread CM
On Jul 19, 11:17 pm, CM wrote: > I have three items in a dict, like this: > > the_dict = {'a':1, 'b':2, 'c':3} > > but the vals could be anything.  I want to configure something else > based on the "winner" of such a dict, with these rules: > > 1. In this dict, if there is a UNIQUE max value, that

Re: Pythonic way with more than one max possible

2011-07-19 Thread Chris Rebert
On Tue, Jul 19, 2011 at 8:17 PM, CM wrote: > I have three items in a dict, like this: > > the_dict = {'a':1, 'b':2, 'c':3} > > but the vals could be anything.  I want to configure something else > based on the "winner" of such a dict, with these rules: > > 1. In this dict, if there is a UNIQUE max

Re: Pythonic way with more than one max possible

2011-07-19 Thread Steven D'Aprano
On Wed, 20 Jul 2011 01:17 pm CM wrote: > I have three items in a dict, like this: > > the_dict = {'a':1, 'b':2, 'c':3} > > but the vals could be anything. I want to configure something else > based on the "winner" of such a dict, with these rules: > > 1. In this dict, if there is a UNIQUE max

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-31 Thread cbr...@cbrownsystems.com
On Oct 31, 4:27 pm, Lawrence D'Oliveiro wrote: > In message > <687bcb76-0093-4d68-ba56-0390a3e1e...@30g2000yql.googlegroups.com>, > > cbr...@cbrownsystems.com wrote: > > I should note that efficiency is not an issue to me here; this is for > > when you have, say, a list user_options of at most aro

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-31 Thread Lawrence D'Oliveiro
In message <687bcb76-0093-4d68-ba56-0390a3e1e...@30g2000yql.googlegroups.com>, cbr...@cbrownsystems.com wrote: > I should note that efficiency is not an issue to me here; this is for > when you have, say, a list user_options of at most around 15 options > or so, and you want to perform some actio

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-31 Thread Lawrence D'Oliveiro
In message <4cca5aaf$0$1600$742ec...@news.sonic.net>, John Nagle wrote: > This is cheaper than intersection ... All together now: “PREMATURE OPTIMIZATION IS THE ROOT OF ALL EVIL!” -- http://mail.python.org/mailman/listinfo/python-list

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-29 Thread cbr...@cbrownsystems.com
On Oct 29, 2:43 am, Steven D'Aprano wrote: > On Thu, 28 Oct 2010 09:16:42 -0700, cbr...@cbrownsystems.com wrote: > > It's clear but tedious to write: > > > if 'monday" in days_off or "tuesday" in days_off: > >     doSomething > > > I currently am tending to write: > > > if any([d for d in ['monday

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-29 Thread Adam Przybyla
cbr...@cbrownsystems.com wrote: > It's clear but tedious to write: > > if 'monday" in days_off or "tuesday" in days_off: >doSomething > > I currently am tending to write: > > if any([d for d in ['monday', 'tuesday'] if d in days_off]): >doSomething > > Is there a better pythonic idiom

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-29 Thread Steven D'Aprano
On Thu, 28 Oct 2010 09:16:42 -0700, cbr...@cbrownsystems.com wrote: > It's clear but tedious to write: > > if 'monday" in days_off or "tuesday" in days_off: > doSomething > > I currently am tending to write: > > if any([d for d in ['monday', 'tuesday'] if d in days_off]): > doSomething

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-29 Thread Xavier Ho
Not sure why you use the for-else syntax without a break or continue. And I'm also not sure on the readability. -Xav on his Froyo On 29/10/2010 6:21 PM, "HEK" wrote: > On Oct 28, 6:16 pm, "cbr...@cbrownsystems.com" > wrote: >> It's clear but tedious to write: >> >> if 'monday" in days_off or "tu

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-29 Thread HEK
On Oct 28, 6:16 pm, "cbr...@cbrownsystems.com" wrote: > It's clear but tedious to write: > > if 'monday" in days_off or "tuesday" in days_off: >     doSomething > > I currently am tending to write: > > if any([d for d in ['monday', 'tuesday'] if d in days_off]): >     doSomething > > Is there a be

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-29 Thread Carl Banks
On Oct 28, 10:50 pm, Paul Rubin wrote: > John Nagle writes: > >    d1 = set('monday','tuesday') > >    days_off = set('saturday','sunday') > >    if not d1.isdisjoint(days_off) :... > >    This is cheaper than intersection, since it doesn't have to > > allocate and construct a set. It just tests

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-29 Thread cbr...@cbrownsystems.com
On Oct 28, 11:56 am, Arnaud Delobelle wrote: > "cbr...@cbrownsystems.com" writes: > > It's clear but tedious to write: > > > if 'monday" in days_off or "tuesday" in days_off: > >     doSomething > > > I currently am tending to write: > > > if any([d for d in ['monday', 'tuesday'] if d in days_off

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-28 Thread Xavier Ho
On 29 October 2010 15:50, Paul Rubin wrote: > John Nagle writes: > >d1 = set('monday','tuesday') > >days_off = set('saturday','sunday') > >if not d1.isdisjoint(days_off) :... > >This is cheaper than intersection, since it doesn't have to > > allocate and construct a set. It just

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-28 Thread Paul Rubin
John Nagle writes: >d1 = set('monday','tuesday') >days_off = set('saturday','sunday') >if not d1.isdisjoint(days_off) :... >This is cheaper than intersection, since it doesn't have to > allocate and construct a set. It just tests whether any element in the > smaller of the two sets

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-28 Thread John Nagle
On 10/28/2010 9:23 AM, John Posner wrote: On 10/28/2010 12:16 PM, cbr...@cbrownsystems.com wrote: It's clear but tedious to write: if 'monday" in days_off or "tuesday" in days_off: doSomething I currently am tending to write: if any([d for d in ['monday', 'tuesday'] if d in days_off]): doSome

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-28 Thread Arnaud Delobelle
"cbr...@cbrownsystems.com" writes: > It's clear but tedious to write: > > if 'monday" in days_off or "tuesday" in days_off: > doSomething > > I currently am tending to write: > > if any([d for d in ['monday', 'tuesday'] if d in days_off]): > doSomething > > Is there a better pythonic idio

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-28 Thread cbr...@cbrownsystems.com
On Oct 28, 10:05 am, Chris Rebert wrote: > On Thu, Oct 28, 2010 at 9:33 AM, cbr...@cbrownsystems.com > > > > wrote: > > On Oct 28, 9:23 am, John Posner wrote: > >> On 10/28/2010 12:16 PM, cbr...@cbrownsystems.com wrote: > > >> > It's clear but tedious to write: > > >> > if 'monday" in days_off o

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-28 Thread nn
On Oct 28, 12:33 pm, "cbr...@cbrownsystems.com" wrote: > On Oct 28, 9:23 am, John Posner wrote: > > > > > On 10/28/2010 12:16 PM, cbr...@cbrownsystems.com wrote: > > > > It's clear but tedious to write: > > > > if 'monday" in days_off or "tuesday" in days_off: > > >      doSomething > > > > I cur

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-28 Thread Chris Rebert
On Thu, Oct 28, 2010 at 9:33 AM, cbr...@cbrownsystems.com wrote: > On Oct 28, 9:23 am, John Posner wrote: >> On 10/28/2010 12:16 PM, cbr...@cbrownsystems.com wrote: >> >> > It's clear but tedious to write: >> >> > if 'monday" in days_off or "tuesday" in days_off: >> >      doSomething >> >> > I c

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-28 Thread Chris Kaynor
On Thu, Oct 28, 2010 at 9:16 AM, cbr...@cbrownsystems.com < cbr...@cbrownsystems.com> wrote: > It's clear but tedious to write: > > if 'monday" in days_off or "tuesday" in days_off: >doSomething > > I currently am tending to write: > > if any([d for d in ['monday', 'tuesday'] if d in days_off]

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-28 Thread cbr...@cbrownsystems.com
On Oct 28, 9:23 am, John Posner wrote: > On 10/28/2010 12:16 PM, cbr...@cbrownsystems.com wrote: > > > It's clear but tedious to write: > > > if 'monday" in days_off or "tuesday" in days_off: > >      doSomething > > > I currently am tending to write: > > > if any([d for d in ['monday', 'tuesday']

Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'

2010-10-28 Thread John Posner
On 10/28/2010 12:16 PM, cbr...@cbrownsystems.com wrote: It's clear but tedious to write: if 'monday" in days_off or "tuesday" in days_off: doSomething I currently am tending to write: if any([d for d in ['monday', 'tuesday'] if d in days_off]): doSomething Is there a better pythonic

Re: Pythonic way to trim and keep leading and trailing whitespace

2010-03-23 Thread Martin P. Hellwig
On 03/23/10 23:38, Tim Chase wrote: Just in case you're okay with a regexp solution, you can use >>> s = "\t\tabc def " >>> import re >>> r = re.compile(r'^(\s*)(.*?)(\s*)$') >>> m = re.match(s) >>> m.groups() ('\t\t', 'abc def', ' ') >>> leading, text, trailing = m.groups() Ahhh regex,

Re: Pythonic way to trim and keep leading and trailing whitespace

2010-03-23 Thread Tim Chase
pyt...@bdurham.com wrote: I'm looking for a pythonic way to trim and keep leading whitespace in a string. Use case: I have a bunch of text strings with various amounts of leading and trailing whitespace (spaces and tabs). I want to grab the leading and trailing whitespace, save it, surround the

Re: Pythonic way to trim and keep leading and trailing whitespace

2010-03-23 Thread Shashwat Anand
regex is not goto that you should always avoid using it. It have its own use-case, here regex solution is intuitive although @emile have done this for you via string manpulation. On Wed, Mar 24, 2010 at 4:09 AM, Daniel Chiquito wrote: > As far as I know, I don't think there is anything that strip

Re: Pythonic way to trim and keep leading and trailing whitespace

2010-03-23 Thread python
uot;Emile van Sebille" To: python-list@python.org Date: Tue, 23 Mar 2010 15:34:48 -0700 Subject: Re: Pythonic way to trim and keep leading and trailing whitespace On 3/23/2010 3:09 PM pyt...@bdurham.com said... > I'm looking for a pythonic way to trim and keep leading > whitespace in

Re: Pythonic way to trim and keep leading and trailing whitespace

2010-03-23 Thread Daniel Chiquito
As far as I know, I don't think there is anything that strips it and returns the material that was stripped. Regex's would be your best bet. Daniel On Tue, Mar 23, 2010 at 6:09 PM, wrote: > I'm looking for a pythonic way to trim and keep leading whitespace in a > string. > > Use case: I have a

Re: Pythonic way to trim and keep leading and trailing whitespace

2010-03-23 Thread Emile van Sebille
On 3/23/2010 3:09 PM pyt...@bdurham.com said... I'm looking for a pythonic way to trim and keep leading whitespace in a string. Use case: I have a bunch of text strings with various amounts of leading and trailing whitespace (spaces and tabs). I want to grab the leading and trailing whitespace,

Re: Pythonic way to overwrite a file

2009-06-18 Thread Tim Wintle
On Wed, 2009-06-17 at 14:26 -0400, Cameron Pulsford wrote: > This is only supposed to handle text files, so when would reading it > all into memory become too much for most computers? I'm guessing there > aren't many source files of too great a size. I often use python with server log files - 1Tb

Re: Pythonic way to overwrite a file

2009-06-17 Thread Rhodri James
Top-posting, tsk, tsk. On Wed, 17 Jun 2009 19:26:07 +0100, Cameron Pulsford wrote: Essentially it just cleans up a source file of erroneous spaces and tabs and can also convert tabs to spaces so loading the whole file into memory is possibly an option. I am making this utility for persona

Re: Pythonic way to overwrite a file

2009-06-17 Thread Cameron Pulsford
Essentially it just cleans up a source file of erroneous spaces and tabs and can also convert tabs to spaces so loading the whole file into memory is possibly an option. I am making this utility for personal use, and that would definitely be fine, but if it turned out well I'd open source it and th

Re: Pythonic way to overwrite a file

2009-06-17 Thread Jean-Michel Pichavant
Cameron Pulsford wrote: Hey all, hopefully a simple question. I'm writing a simple python tool that opens a file, and does something like for line in file.readlines(): temp.write(line.doStuff()) However, I want to provide the option do this "in place", as in have the destination file be

Re: Pythonic way to overwrite a file

2009-06-17 Thread Ben Charrow
Cameron Pulsford wrote: > Hey all, hopefully a simple question. > > I'm writing a simple python tool that opens a file, and does something like > > for line in file.readlines(): > temp.write(line.doStuff()) > > However, I want to provide the option do this "in place", as in have the > desti

Re: Pythonic way to normalize vertical whitespace

2009-05-10 Thread Stephen Hansen
On Fri, May 8, 2009 at 8:53 AM, wrote: > I'm looking for suggestions on technique (not necessarily code) about the > most pythonic way to normalize vertical whitespace in blocks of text so that > there is never more than 1 blank line between paragraphs. Our source text > has newlines normalized

Re: Pythonic way to determine if one char of many in a string

2009-02-21 Thread odeits
On Feb 21, 2:24 pm, rdmur...@bitdance.com wrote: > odeits wrote: > > On Feb 21, 12:47=A0am, "Gabriel Genellina" > > wrote: > > > En Sat, 21 Feb 2009 01:14:02 -0200, odeits escribi=F3: > > > > > On Feb 15, 11:31=A0pm, odeits wrote: > > > >> It seems what you are actually testing for is if the in

Re: Pythonic way to determine if one char of many in a string

2009-02-21 Thread rdmurray
odeits wrote: > On Feb 21, 12:47=A0am, "Gabriel Genellina" > wrote: > > En Sat, 21 Feb 2009 01:14:02 -0200, odeits escribi=F3: > > > > > On Feb 15, 11:31=A0pm, odeits wrote: > > >> It seems what you are actually testing for is if the intersection of > > >> the two sets is not empty where the fi

Re: Pythonic way to determine if one char of many in a string

2009-02-21 Thread odeits
On Feb 21, 12:47 am, "Gabriel Genellina" wrote: > En Sat, 21 Feb 2009 01:14:02 -0200, odeits escribió: > > > > > On Feb 15, 11:31 pm, odeits wrote: > >> It seems what you are actually testing for is if the intersection of > >> the two sets is not empty where the first set is the characters in >

Re: Pythonic way to determine if one char of many in a string

2009-02-21 Thread Gabriel Genellina
En Sat, 21 Feb 2009 01:14:02 -0200, odeits escribió: On Feb 15, 11:31 pm, odeits wrote: It seems what you are actually testing for is if the intersection of the two sets is not empty where the first set is the characters in your word and the second set is the characters in your defined strin

Re: Pythonic way to determine if one char of many in a string

2009-02-20 Thread odeits
On Feb 15, 11:31 pm, odeits wrote: > On Feb 15, 9:56 pm, Chris Rebert wrote: > > > > > On Sun, Feb 15, 2009 at 9:17 PM,   wrote: > > > I need to test strings to determine if one of a list of chars is in the > > > string. A simple example would be to test strings to determine if they > > > have >

Re: Pythonic way to determine if one char of many in a string

2009-02-19 Thread Martin P. Hellwig
MRAB wrote: Dennis Lee Bieber wrote: On Wed, 18 Feb 2009 21:22:45 +0100, Peter Otten <__pete...@web.de> declaimed the following in comp.lang.python: Steve Holden wrote: Jervis Whitley wrote: What happens when you have hundreds of megabytes, I don't know. I hope I never have to test a wor

Re: Pythonic way to determine if one char of many in a string

2009-02-19 Thread MRAB
Dennis Lee Bieber wrote: On Wed, 18 Feb 2009 21:22:45 +0100, Peter Otten <__pete...@web.de> declaimed the following in comp.lang.python: Steve Holden wrote: Jervis Whitley wrote: What happens when you have hundreds of megabytes, I don't know. I hope I never have to test a word that is hun

Re: Pythonic way to determine if one char of many in a string

2009-02-19 Thread John Machin
On Feb 19, 6:47 pm, Dennis Lee Bieber wrote: > On Wed, 18 Feb 2009 21:22:45 +0100, Peter Otten <__pete...@web.de> > declaimed the following in comp.lang.python: > > > Steve Holden wrote: > > > > Jervis Whitley wrote: > > >>> What happens when you have hundreds of megabytes, I don't know. > > > >>

Re: Pythonic way to determine if one char of many in a string

2009-02-18 Thread Peter Otten
Steve Holden wrote: > Jervis Whitley wrote: >>> What happens when you have hundreds of megabytes, I don't know. >>> >>> >> I hope I never have to test a word that is hundreds of megabytes long >> for a vowel :) > > I see you don't speak German ;-) I tried to come up with a funny way to point out

Re: Pythonic way to determine if one char of many in a string

2009-02-18 Thread Peter Otten
Steven D'Aprano wrote: > On Wed, 18 Feb 2009 07:08:04 +1100, Jervis Whitley wrote: > > >>> This moves the for-loop out of slow Python into fast C and should be >>> much, much faster for very large input. >>> >>> >> _Should_ be faster. > > Yes, Python's timing results are often unintuitive. Ind

Re: Pythonic way to determine if one char of many in a string

2009-02-18 Thread Steve Holden
Jervis Whitley wrote: >> What happens when you have hundreds of megabytes, I don't know. >> >> > I hope I never have to test a word that is hundreds of megabytes long > for a vowel :) I see you don't speak German ;-) -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC

Re: Pythonic way to determine if one char of many in a string

2009-02-18 Thread Jervis Whitley
> > What happens when you have hundreds of megabytes, I don't know. > > I hope I never have to test a word that is hundreds of megabytes long for a vowel :) -- http://mail.python.org/mailman/listinfo/python-list

Re: Pythonic way to determine if one char of many in a string

2009-02-18 Thread Steven D'Aprano
On Wed, 18 Feb 2009 07:08:04 +1100, Jervis Whitley wrote: >> This moves the for-loop out of slow Python into fast C and should be >> much, much faster for very large input. >> >> > _Should_ be faster. Yes, Python's timing results are often unintuitive. > Here is my test on an XP system Python

Re: Pythonic way to determine if one char of many in a string

2009-02-17 Thread Jervis Whitley
> > This moves the for-loop out of slow Python into fast C and should be much, > much faster for very large input. > _Should_ be faster. Here is my test on an XP system Python 2.5.4. I had similar results on python 2.7 trunk. WORD = 'g' * 100 WORD2 = 'g' * 50 + 'U' BIGWORD = 'g' * 1 + 'U'

Re: Pythonic way to determine if a string is a number

2009-02-17 Thread python
Original poster here: Just for the record, my *original* post did include an explicit trapping of the ValueError exception. :) My point is that the coaching offered by this forum does not always fall on deaf ears. Thanks for everyone's help on this and all the other posts in this forum. Regards

Re: Pythonic way to determine if a string is a number

2009-02-17 Thread Paddy O'Loughlin
2009/2/16 Python Nutter : > silly me, forgot to mention > > build a set from digits + '.' and use that for testing. > > Cheers, > PN > > > 2009/2/16 Python Nutter : >> Type casting seems to be the wrong way to go about this. >> >> teststring = '15719' >> teststring.isdigit() >> returns True >> >> T

Re: Pythonic way to determine if a string is a number

2009-02-17 Thread Floris Bruynooghe
On Feb 16, 7:09 am, Python Nutter wrote: > silly me, forgot to mention > > build a set from digits + '.' and use that for testing. `.' is locale dependent. Some locales might use `,' instead and maybe there's even more out there that I don't know of. So developing this yourself from scratch see

Re: Pythonic way to determine if a string is a number

2009-02-17 Thread Floris Bruynooghe
On Feb 16, 12:05 am, Mel wrote: > Christian Heimes wrote: > > Roy Smith wrote: > >> They make sense when you need to recover from any error that may occur, > >> possibly as the last resort after catching and dealing with more specific > >> exceptions. In an unattended embedded system (think Mars R

Re: Pythonic way to determine if a string is a number

2009-02-17 Thread Roy Smith
In article <01aaa1da$0$20629$c3e8...@news.astraweb.com>, Steven D'Aprano wrote: > Okay, but that surely falls under chapter 18 of the "Advanced Python > Programming for the Mars Rover" book rather than chapter 5 of "Newbies > Guide to Python". Well, sure, but this thread started out with my tak

Re: Pythonic way to determine if a string is a number

2009-02-17 Thread Steven D'Aprano
Roy Smith wrote: >> > Do you really want to except SystemExit, KeyboardInterrupt, MemoryError >> > and SyntaxError? > > Absolutely. Let's take my example -- you're writing software for a Mars > Rover. I have no idea how you might get a MemoryError, but let's say you > do. Which would you rathe

Re: Pythonic way to determine if one char of many in a string

2009-02-17 Thread Steven D'Aprano
Nicolas Dandrimont wrote: > I would go for something like: > > for char in word: > if char in 'aeiouAEIUO': > char_found = True > break > else: > char_found = False > > (No, I did not forget to indent the else statement, see > http://docs.python.org/reference/compound_stm

Re: Pythonic way to determine if a string is a number

2009-02-16 Thread Paddy
On Feb 15, 5:46 pm, pyt...@bdurham.com wrote: > What's the Pythonic way to determine if a string is a number? By > number I mean a valid integer or float. > > I searched the string and cMath libraries for a similar function > without success. I can think of at least 3 or 4 ways to build my > own fu

Re: Pythonic way to determine if a string is a number

2009-02-16 Thread Nick Craig-Wood
pyt...@bdurham.com wrote: > Thanks for everyone's feedback. I believe my original post's code > (updated following my signature) was in line with this list's feedback. > > Christian: Thanks for reminding me about exponential formats. My updated > code accounts for these type of numbers. I don

Re: Pythonic way to determine if one char of many in a string

2009-02-16 Thread Stefaan Himpe
An entirely different approach would be to use a regular expression: import re if re.search("[abc]", "nothing expekted"): print "a, b or c occurs in the string 'nothing expekted'" if re.search("[abc]", "something expected"): print "a, b or c occurs in the string 'something expected'" Best

Re: Pythonic way to determine if one char of many in a string

2009-02-16 Thread J. Cliff Dyer
On Mon, 2009-02-16 at 00:28 -0500, Nicolas Dandrimont wrote: > * pyt...@bdurham.com [2009-02-16 00:17:37 -0500]: > > > I need to test strings to determine if one of a list of chars is > > in the string. A simple example would be to test strings to > > determine if they have a vowel (aeiouAEIOU)

Re: Pythonic way to determine if one char of many in a string

2009-02-16 Thread Nicolas Dandrimont
* pyt...@bdurham.com [2009-02-16 00:48:34 -0500]: > Nicolas, > > > I would go for something like: > > > > for char in word: > > if char in 'aeiouAEIUO': > > char_found = True > > break > > else: > > char_found = False > > > > It is clear (imo), and it is seems to be the

[Fwd: Re: Pythonic way to determine if a string is a number]

2009-02-16 Thread Tim Golden
[Resending after a bounce from mailing list] pyt...@bdurham.com wrote: try: float (input) except ValueError: return False else: return True I follow the semantics, but I don't know why I would prefer the try/else technique over the simpler: try: float( input ) return True exc

Re: Pythonic way to determine if one char of many in a string

2009-02-15 Thread odeits
On Feb 15, 9:56 pm, Chris Rebert wrote: > On Sun, Feb 15, 2009 at 9:17 PM,   wrote: > > I need to test strings to determine if one of a list of chars is in the > > string. A simple example would be to test strings to determine if they have > > a vowel (aeiouAEIOU) present. > > > I was hopeful that

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Tino Wildenhain
Roy Smith wrote: In article , Mel wrote: Christian Heimes wrote: Roy Smith wrote: They make sense when you need to recover from any error that may occur, possibly as the last resort after catching and dealing with more specific exceptions. In an unattended embedded system (think Mars Rover)

  1   2   >