Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > List question

Reply
Thread Tools

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-----

 
Reply With Quote
 
 
 
 
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: ...
 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
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
 
Reply With Quote
 
Zentrader
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
 
Reply With Quote
 
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

 
Reply With Quote
 
Zentrader
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.
 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      03-22-2008
Zentrader wrote:
> 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.
 
Reply With Quote
 
Zentrader
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.
 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Collections List : Converting from List '<Column <String1,String2>>' to 'List <String1>' asil klin Java 28 03-05-2011 01:59 AM
Memory issues when storing as List of Strings vs List of List OW Ghim Siong Python 2 11-30-2010 12:22 PM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
Why does list.__getitem__ return a list instance for subclasses ofthe list type? dackz Python 0 02-06-2007 04:44 PM
Difference Between List x; and List x(); , if 'List' is a Class? roopa C++ 6 08-27-2004 06:18 PM



Advertisments