By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

List question

P: n/a
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
Mar 21 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On 2008-03-21 05:16:41 PM, Fr**@home.xs4all.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-----

Mar 21 '08 #2

P: n/a
From@home writes:
if 'one' and 'two' in f:
alist.append(f)
Use:
if 'one' in f and 'two' in f: ...
Mar 21 '08 #3

P: n/a
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
Mar 22 '08 #4

P: n/a
On Mar 22, 4:38*pm, Zentrader <zentrad...@gmail.comwrote:
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

Mar 22 '08 #5

P: n/a
On Mar 22, 10:07 am, Arnaud Delobelle <arno...@googlemail.comwrote:
On Mar 22, 4:38 pm, Zentrader <zentrad...@gmail.comwrote:
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.
Mar 22 '08 #6

P: n/a
Zentrader wrote:
On Mar 22, 10:07 am, Arnaud Delobelle <arno...@googlemail.comwrote:
>On Mar 22, 4:38 pm, Zentrader <zentrad...@gmail.comwrote:
>>>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.
Mar 22 '08 #7

P: n/a
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.
Mar 22 '08 #8

P: n/a
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
Mar 22 '08 #9

P: n/a
On Sat, 22 Mar 2008 16:16:03 -0700, bearophileHUGS wrote:
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.
There is nothing wrong with what you said originally. You said "syntax
MAY be" -- that's obvious to those who know English that you are talking
about a hypothetical keyword.

Admittedly non-English speakers may not pick up on the nuances of
English, but that's not your fault.

Your English is very good, better than some native English speakers. What
is your native language?
--
Steven
Mar 22 '08 #10

P: n/a
On Sat, 22 Mar 2008 14:55:39 -0700, Zentrader wrote:
>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.

Oh stop your whining. Nobody is making fun of anyone, or laughing at
anyone, although I'm just about ready to start laughing at *you* for your
completely unjustified conviction that people are being cruel.

Bearophile made a clearly hypothetical comment using perfectly valid and
understandable English. That you misunderstood it isn't anybody's fault
except your own. Get over it, stop trying to blame others, and move on.
You're not the victim here, and Bearophile isn't the bad guy.

--
Steven
Mar 23 '08 #11

P: n/a
On Mar 22, 8:40�pm, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On Sat, 22 Mar 2008 14:55:39 -0700 (PDT), Zentrader
<zentrad...@gmail.comdeclaimed the following in comp.lang.python:
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

� � � � Rhett Butler, I believe (I only know two characters by name,
You don't remember Ashley?

I guess that's only half a name.
and
since the line as I recall it is "Frankly, Scarlett, I ..." it couldn't
have been her [Scarlett] speaking)
--
� � � � Wulfraed � � � �Dennis Lee Bieber � � � � � � � KD6MOG
� � � � wlfr...@ix.netcom.com � � � � � � wulfr...@bestiaria.com
� � � � � � � � HTTP://wlfraed.home.netcom.com/
� � � � (Bestiaria Support Staff: � � � � � � � web-a...@bestiaria.com)
� � � � � � � � HTTP://www.bestiaria.com/
Mar 23 '08 #12

P: n/a
On Mar 21, 3:57 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
From@home writes:
if 'one' and 'two' in f:
alist.append(f)

Use:
if 'one' in f and 'two' in f: ...
Personally, I would put parentheses around to be clearer:

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

I'm not saying to put parentheses around everything, but in the more
ambiguous cases, it certainly helps.
Mar 23 '08 #13

P: n/a
On Mar 24, 12:19 am, Dustan <DustanGro...@gmail.comwrote:
On Mar 21, 3:57 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
From@home writes:
if 'one' and 'two' in f:
alist.append(f)
Use:
if 'one' in f and 'two' in f: ...

Personally, I would put parentheses around to be clearer:

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

I'm not saying to put parentheses around everything, but in the more
ambiguous cases, it certainly helps.
Please help me understand why this is a "more ambiguous" case.

To me, alternative interpretations have extremely low scores for
utility and likelihood:

(1) 'and' has higher precedence than 'in':

'one' in (f and 'two') in f # chained (x in y in z)

(2) 'and' has same precedence as 'in':

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

Cheers,
John
Mar 23 '08 #14

P: n/a
On Sun, 23 Mar 2008 15:55:43 -0700, John Machin wrote:
On Mar 24, 12:19 am, Dustan <DustanGro...@gmail.comwrote:
>On Mar 21, 3:57 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
From@home writes:
if 'one' and 'two' in f:
alist.append(f)
Use:
if 'one' in f and 'two' in f: ...

Personally, I would put parentheses around to be clearer:

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

I'm not saying to put parentheses around everything, but in the more
ambiguous cases, it certainly helps.

Please help me understand why this is a "more ambiguous" case.
Some people don't remember, and have no interest in forcing the people
who read their code to memorize, the relative precedence of 'in' and
'and', and so don't expect folks to understand whether

'one' in f and 'two' in f

means

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

or

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

or something else.

This especially holds for people who have used languages where the
precedences are less sensible than they are in Python. By memory,
standard Pascal uses the second interpretation if you don't use
parentheses, which is stupid, but there you go.

To me, alternative interpretations have extremely low scores for
utility and likelihood:
Outside of the interactive interpreter, I prefer to know what my code
does before I write it, rather than guess that it's unlikely to be wrong.

(Or at least *think* I know what it does, which is not always the same
thing as knowing what it does.)

--
Steven
Mar 24 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.