Velocity Reviews > List question

# List question

Ricky Zhou
Guest
Posts: n/a

 03-21-2008
On 2008-03-21 05:16:41 PM, http://www.velocityreviews.com/forums/(E-Mail Removed)4all.nl wrote:
> alist = []
> blist = [ 'one','two','one and two','one and four','five','one two']
> for f in blist:
> if 'one' and 'two' in f:
> alist.append(f)
>
> for i in alist:
> print i
>
> two
> one and two
> one two
>
>
> why is it printing the first "two"?

Look at the this line:

if 'one' and 'two' in f:

You're basically saying:

if ('one') and ('two' in f):

which is why you only get elements that contain 'two'.

Ricky

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFH5CfQZBKKLMyvSE4RAmDdAJ4hdhYKo7sijC3hlHWGvH 23Z+kswwCfXReH
1kfE+VFM1rfHnXFIDjGGFKU=
=PLdZ
-----END PGP SIGNATURE-----

Paul Rubin
Guest
Posts: n/a

 03-21-2008
From@home writes:
> if 'one' and 'two' in f:
> alist.append(f)

Use:
if 'one' in f and 'two' in f: ...

From@home
Guest
Posts: n/a

 03-21-2008
Hello,

I am learning python and dont quuite understand why is this happening
could someone explain?

alist = []
blist = [ 'one','two','one and two','one and four','five','one two']
for f in blist:
if 'one' and 'two' in f:
alist.append(f)

for i in alist:
print i

two
one and two
one two

why is it printing the first "two"?

tia

bearophileHUGS@lycos.com
Guest
Posts: n/a

 03-21-2008
Ricky Zhou:
> Look at the this line:
> if 'one' and 'two' in f:

Very cute, it's the first time I see a bug like this. I think it's not
a common enough pattern to justify a language change, but a bit
smarter computer language may be able to do that too (it's not
easy to tell the two meanings apart, from a simple point of view the
semantics is ambiguous) Maybe an AppleScript-like language can be
designed to have such capabilities too

A more computer-friendly (and Pythonic) syntax may be ('are' is a
keyword):
if ('one', 'two') are in f: ...
That's sugar for:
if all(x in f for x in ('one', 'two')): ...

Bye,
bearophile

Guest
Posts: n/a

 03-22-2008
> if ('one', 'two') are in f: ...

"are" gives me an error in Python 2.5 with a "from future import *"
statement included. What version and platform are you running. Also,
the docs don't mention it.
http://docs.python.org/ref/keywords.html

Arnaud Delobelle
Guest
Posts: n/a

 03-22-2008
On Mar 22, 4:38*pm, Zentrader <(E-Mail Removed)> wrote:
> > if ('one', 'two') are in f: ...

>
> "are" gives me an error in Python 2.5 with a "from future import *"
> statement included. *What version and platform are you running. *Also,
> the docs don't mention it.http://docs.python.org/ref/keywords.html

That's because you have to do:

from bearophile import musings

HTH

--
Arnaud

Guest
Posts: n/a

 03-22-2008
On Mar 22, 10:07 am, Arnaud Delobelle <(E-Mail Removed)> wrote:
> On Mar 22, 4:38 pm, Zentrader <(E-Mail Removed)> wrote:
>
> > > if ('one', 'two') are in f: ...

>
> > "are" gives me an error in Python 2.5 with a "from future import *"
> > statement included. What version and platform are you running. Also,
> > the docs don't mention it.http://docs.python.org/ref/keywords.html

>
> That's because you have to do:
>
> from bearophile import musings
>
> HTH
>
> --
> Arnaud

Thanks. I am admittedly naive and don't have any type of guard up
when on this group for people who think that type of comment makes
them intelligent/funny.

Jeff Schwab
Guest
Posts: n/a

 03-22-2008
> On Mar 22, 10:07 am, Arnaud Delobelle <(E-Mail Removed)> wrote:
>> On Mar 22, 4:38 pm, Zentrader <(E-Mail Removed)> wrote:
>>
>>>> if ('one', 'two') are in f: ...
>>> "are" gives me an error in Python 2.5 with a "from future import *"
>>> statement included. What version and platform are you running. Also,
>>> the docs don't mention it.http://docs.python.org/ref/keywords.html

>> That's because you have to do:
>>
>> from bearophile import musings
>>
>> HTH
>>
>> --
>> Arnaud

>
> Thanks. I am admittedly naive and don't have any type of guard up
> when on this group for people who think that type of comment makes
> them intelligent/funny.

No one meant to laugh at you. Your naivete was not obvious. FWIW, a
sense of humor is a valuable possession in most Python-related
conversations.

Guest
Posts: n/a

 03-22-2008
> No one meant to laugh at you. Your naivete was not obvious. FWIW, a
> sense of humor is a valuable possession in most Python-related
> conversations.

Perhaps someone can explain how telling something like this to the OP,
who thinks this statement will work
if 'one' and 'two' in f:
is funny and not mean. In the words of whoever it was in "Gone With
The Wind", frankly I don't give a damn (except to not mislead relative
newbies). But this has wasted enough of everyone's time.

bearophileHUGS@lycos.com
Guest
Posts: n/a

 03-22-2008
bearophile:
> A more computer-friendly (and Pythonic) syntax may be ('are' is a keyword):

Sorry for causing confusion, I was just thinking aloud. English isn't
my first language, and sometimes I slip a bit. Replace that with:

> A more computer-friendly (and Pythonic) syntax may be ('are' is meant to be a keyword in such hypothetical situation):

Bye,
bearophile