-3

I was using the if any(word in 'x' for word in list): into a cycle in order to know if any of some words (in a list) were in specific texts in order to discard the ones for which none of the words was present. It used to work pretty fine but, from a couple of months ago the conditional expression starts to give always positive results.

For example:

list=['home','cat']
if any(word in "my home is red" for word in list):
    print "YES" 
YES
if any(word in "my hair is red" for word in list):
    print "YES" 
YES

But the second conditional is supposed to show me a negative result. I didn't change anything in my previous code but anyway I can be making some stupid mistake which someone could help me?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 4
    This code is running as I would expect it would. The first `any` returns `True `and the second returns `False` – idjaw Apr 01 '16 at 02:59
  • 4
    Are you using NumPy or a setup configured to auto-run `from numpy import *`? – user2357112 Apr 01 '16 at 02:59
  • as a general rule, it is bad practice to override a reserved word as you have done with `list`. try to use a more descriptive name. – Haleemur Ali Apr 01 '16 at 03:18
  • Side-note: Don't name variables `list`; it's a great way to get really confused when you try to convert some other iterable to a `list` and oops, `list` isn't the `list` constructor anymore. – ShadowRanger Apr 01 '16 at 03:18
  • @HaleemurAli: `list` isn't technically "reserved", it's just a built-in name. [Very few names in Python are actually restricted](https://docs.python.org/3/library/keyword.html), it's just a bad idea to shadow the built-ins. – ShadowRanger Apr 01 '16 at 03:19
  • @idjaw: Except they're claiming both return `True`... That said, I don't see any way for this to be the case unless they're mistaken as to the contents of `list`. This smells like user error that the minimal example doesn't display. – ShadowRanger Apr 01 '16 at 03:21
  • 1
    It doesn't look like the questioner stuck around. I'm dupe-hammering based on the probable NumPy explanation. If it turns out it's not using `numpy.any`, we can always reopen. – user2357112 Apr 01 '16 at 03:37

1 Answers1

0

Are you sure the second "YES" comes from the second line? Change your code to

mylist=['home','cat']
if any(word in "my home is red" for word in mylist):
    print "YES" 
else:
    print "NO"

if any(word in "my hair is red" for word in mylist):
    print "YES" 
else:
    print "NO"

When I run this, I get

YES
NO
Sci Prog
  • 2,651
  • 1
  • 10
  • 18