-1

I'm trying to search words in a text with the function any() of python, I was expecting the same behaviour for the two following, but it's not:

keyword_list=['WASHER', 'SCREW']
all_text  = "test_string"
if any(word in all_text for word in keyword_list):
    print "some of the strings found in str"
else:
    print "no strings found in str"

if (True in (word in all_text for word in keyword_list)):
    print "some of the strings found in str"
else:
    print "no strings found in str"

running inside Spyder 2.2.0 with python 2.7.5(I downloaded the pythonXY package) the results are different from launching python with normal cmd console.

In fact result inside Spyder:

>>> runfile(r'C:\untitled0.py', wdir=r'C:\PythonTestbench')
some of the strings found in str
no strings found in str

In fact result inside command prompt:

>>> runfile(r'C:\untitled0.py', wdir=r'C:\PythonTestbench')
no strings found in str
no strings found in str

Has Spyder environment has another version of any() function, as explained here.

How can I force to not use the function contained into NumPy?

Community
  • 1
  • 1
tinix84
  • 57
  • 5
  • 1
    Both versions print `no strings found in str`. What's wrong? – falsetru Jan 16 '14 at 12:48
  • Both yield False—What is your issue? – poke Jan 16 '14 at 12:48
  • works for me, also if I change 'SCREW' into 'string' so they return True – RemcoGerlich Jan 16 '14 at 12:49
  • Hey, I can reproduce this behaviour. Found it for python version 2.7.3 in IPython Notebook, oddly it works in the IPython shell. On another computer I am having Python version 2.7.5 and there it is working either way. @tinix84 How did you run your code? – asPlankBridge Jan 16 '14 at 13:21
  • I modified above with a better explanation, thanks guys – tinix84 Jan 16 '14 at 14:01
  • Okay - probably not the answer you're looking for, but see this [bug report on github](https://github.com/ipython/ipython/issues/2598/). Maybe your any() function has been replaced as well... @tinix84 What is the output if you run `print any`? it should be ``... – asPlankBridge Jan 16 '14 at 14:03
  • 4
    Yeah, this is because PythonXY imports numpy's `all` and `any` into scope, clobbering the builtins. See my [answer here](http://stackoverflow.com/a/7493265/487339). – DSM Jan 16 '14 at 15:06
  • @asPlankBridge my printout of any is : – tinix84 Jan 17 '14 at 12:36
  • Okay, I get a similar output for the numpy any variant. If it is numpy the output of `print help(any)` should mention `module numpy.core.fromnumeric` in the first line. One solution is to use `__builtin__.any()` or write at the top of your script `from __builtin__ import any` or use another Editor. :) – asPlankBridge Jan 17 '14 at 13:44

1 Answers1

0

Modify your code in this way:

keyword_list=['WASHER', 'SCREW']
all_text  = "test_string"
print any # so we can see where it comes from
mkgen = lambda: (word in all_text for word in keyword_list)
print list(mkgen()) # to see what we have

if any(mkgen()):
    print "any: some of the strings found in str"
else:
    print "any: no strings found in str"

if (True in mkgen()):
    print "in: some of the strings found in str"
else:
    print "in: no strings found in str"

This program makes it apparent what happens:

  • it prints out the nature of any so you see if it is the "normal one" or a version overwritten by some import *
  • it as well prints out the boolean values generated

If you have managed to find out what's wrong, please use the any() variant. It is much more pythonic.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Hi, this doesn't change my final result: the printout is `[False, False] any: some of the strings found in str in: no strings found in str` – tinix84 Jan 17 '14 at 12:37
  • @tinix84 You are missing the first line of the printout: what does `print any` give? This is essential, as t shows us if it comes from Python itself or from a 3rd party module. – glglgl Jan 17 '14 at 12:45
  • my printout of any is : – tinix84 Jan 18 '14 at 14:48