Re: [Tutor] How to determine if every character in one string is inanother string?

2007-07-25 Thread Kent Johnson
Alan Gauld wrote:
> "Terry Carroll" <[EMAIL PROTECTED]> wrote
> 
>>   if Y in X:
> 
> FWIW I believe that 'in' did only work for single characters up until
> version 2.X so your ideas may have been based on experiences with
> an earlier Python version.

Yes, it changed in Python 2.3.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine if every character in one string is inanother string?

2007-07-25 Thread Alan Gauld

"Terry Carroll" <[EMAIL PROTECTED]> wrote

>   if Y in X:
>
> Which is much more elegant/pythonic; but I didn't know you could do 
> that
> with one string over another.  For some reason, I had thought Y 
> would have
> to exactly match one iterable element in X (e.g., one element of a 
> list,
> or one character of a string) for that to work.

FWIW I believe that 'in' did only work for single characters up until
version 2.X so your ideas may have been based on experiences with
an earlier Python version.

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine if every character in one string is inanother string?

2007-07-25 Thread Terry Carroll
On Tue, 24 Jul 2007, wesley chun wrote:

> i don't have any time myself either (getting ready for OSCON talk),
> but i'm not sure what terry's OP was about... looking for a
> well-written piece of code, a faster-performing snippet, or both?  i
> think he was just unsatissfied with his 1st attempt.

Exactly.  It worked fine, but just seemed unpythonic to me.

To use an analogy, not long ago I thought the best way to see if string X 
contained string Y was:

   if X.find(Y) != -1

Which works just fine.  But another poster pointed out:

   if Y in X:

Which is much more elegant/pythonic; but I didn't know you could do that
with one string over another.  For some reason, I had thought Y would have
to exactly match one iterable element in X (e.g., one element of a list,
or one character of a string) for that to work.

Similarly, I was thinking that while that first attempt of mine worked, 
its apparent-to-me lameness suggested that there was a more idiomatic 
approach, and I wanted to find out what that was.  

Seeing the different approaches put forward by the various contributors
was pretty interesting, though, I must say.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine if every character in one string is inanother string?

2007-07-24 Thread wesley chun
> any() and all() also short-circuit, and they move the iteration out of
> Python into the C runtime. My guess is the solutions with any() and
> all() and equivalent hoisting of string.printable will be faster than
> yours, but I don't want to take the time to check ATM...I would also try
> using set(string.printable) instead of string.printable...

if they do, yeah, that'll be faster since it'll run in C.  you're
probably also right about set since it doesn't have to "look into a
string" to see whether it has a character or not.  (this is the same
as the advantages that a hash table has over an array.).


> Anyone feel like spending some time with timeit? Otherwise we're just
> guessing anyway.

i don't have any time myself either (getting ready for OSCON talk),
but i'm not sure what terry's OP was about... looking for a
well-written piece of code, a faster-performing snippet, or both?  i
think he was just unsatissfied with his 1st attempt.

-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine if every character in one string is inanother string?

2007-07-23 Thread Kent Johnson
wesley chun wrote:

> from string import printable as prglobal
> def printable(s):
> prlocal = prglobal
> for x in s:
> if x not in prlocal:
> return False
> return True
> 
> the solutions using LCs above are great when it comes to an expressive
> piece of code in a one-liner, but i feel there's a waste of
> time/memory.  the use of GEs is better, but it still has to iterate
> over the entire string when i don't feel that it should be necessary
> as per my example. anyway, just my $0.02! not the shortest, but
> hopefully the fastest!

any() and all() also short-circuit, and they move the iteration out of 
Python into the C runtime. My guess is the solutions with any() and 
all() and equivalent hoisting of string.printable will be faster than 
yours, but I don't want to take the time to check ATM...I would also try 
using set(string.printable) instead of string.printable...

Anyone feel like spending some time with timeit? Otherwise we're just 
guessing anyway.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine if every character in one string is inanother string?

2007-07-23 Thread wesley chun
> > > > all(char in string.printable for char in testString)
>
> That was my favorite, too.  I didn't notice the new all method in 2.5.  It
> certainly seems the most Pythonic approach.


all() has a sister built-in function, also introduced in 2.5, so i
think that any(char not in string.printable for char in testString)
will work too.

however, believe it or not, i kinda like your original answer... it's
not as lame as you think! :-) i tweaked it slightly (for performance
reasons) to:

from string import printable as prglobal
def printable(s):
prlocal = prglobal
for x in s:
if x not in prlocal:
return False
return True

- i got rid of the list comp and the list comparisons... they're not
necessary and slow things down

- i moved the import outside the function... not sure why i did this.
must be leftover from the days i used to think that the import would
occur every time the function's called, but now i know that there's a
difference between "importing" and "loading" of a module!

- i also created a local reference to string.printable to speed up the
lookup -- recall the name resolution order is: locals, globals,
built-ins

- i also also short-circuit the rest of the string if/when it finds
the 1st non-printable. (no need to go on in a 10-million character
string if char#2 is a non-printable right?)

the solutions using LCs above are great when it comes to an expressive
piece of code in a one-liner, but i feel there's a waste of
time/memory.  the use of GEs is better, but it still has to iterate
over the entire string when i don't feel that it should be necessary
as per my example. anyway, just my $0.02! not the shortest, but
hopefully the fastest!

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine if every character in one string is inanother string?

2007-07-23 Thread Terry Carroll
On Sun, 22 Jul 2007, Jerry Hill wrote:

> On 7/21/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
> > > all(char in string.printable for char in testString)
> >
> > What is all?
> > Is that a 2.5 thing (I'm still on 2.4 here)
> 
> Yes, it's a 2.5 thing.

That was my favorite, too.  I didn't notice the new all method in 2.5.  It 
certainly seems the most Pythonic approach.

I really enjoyed seeing all the proposed alternatives, however.  Thanks, 
everybody!


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine if every character in one string is inanother string?

2007-07-22 Thread Jerry Hill
On 7/21/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
> > all(char in string.printable for char in testString)
>
> What is all?
> Is that a 2.5 thing (I'm still on 2.4 here)

Yes, it's a 2.5 thing.  All returns true if all of the elements of an
iterable are true.  According to the docs, it is the equivalent of the
following:

 def all(iterable):
 for element in iterable:
 if not element:
 return False
 return True

Using 'all' with a generator expression has the virtue of only needing
to look at the string until it finds a single element that is false,
and then returning.  Not only that, it's concise (a single line) and
quite readable (at least to me).

-- 
Jerry
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine if every character in one string is inanother string?

2007-07-21 Thread Alan Gauld

"Jerry Hill" <[EMAIL PROTECTED]> wrote 

> I like this one:
> all(char in string.printable for char in testString)

What is all?
Is that a 2.5 thing (I'm still on 2.4 here)

 testString = "qwerty\buiop"
 all(char in string.printable for char in testString)
> False
> 
> -- 
> Jerry
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor