PROBLEMS WITH PYTHON IN SOME VARIABLE,FUNCTIONS,ETC.

Robert Bossy Robert.Bossy at jouy.inra.fr
Tue Apr 8 11:40:06 EDT 2008


Hi,

First thing, I appreciate (and I'm positive we all do) if you DID'N YELL 
AT ME.

subhabrata.iisc at hotmail.com wrote:
> I am using Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.
> 1310 32 bit (Intel)] on win32 with IDLE 1.2.1
> My O/S is Windows XP SP2 I use 512 MB RAM.
> I am encountering the following problems:
> (i) a1=1
>     a2=2
>     a3=a1+a2
>     print a3
> # The result is coming sometimes as 3 sometimes as vague numbers.
>   
On all computers I work with (two at work and one at home), it always 
gives me 3.

> (ii) x1="Bangalore is called the Silicon Valley of India"
>     x2="NewYork"
>     x3=x1.find(x2)
>     print x3
> # The result of x3 is coming as -1 as well as +ve numbers.
>   
On my computer, this always gives me -1 which is what I expected since 
x2 not in x1.
Are you sure you posted what you wanted to show us?

> (iii) I have been designing one crawler using "urllib". For crawling
> one web page it is perfect. But when I am giving around 100 URLs by
> and their links and sublinks the IDLE is not responding. Presently I
> have been running with 10 URLs but can't it be ported?
>   
Maybe you've implemented quadratic algorithms, or even exponential. 
Sorry, I cannot see without more specifics...

> (iv) I have designed a program with more than 500 if elif else but
> sometimes it is running fine sometimes it is giving hugely erroneous
> results, one view of the code:
> elif a4==3:
>         print "YOU HAVE NOW ENTERED THREE WORDS"
>         if a3[0] not in a6:
>             if a3[1] not in a6:
>                 if a3[2] not in a6:
>                     print "a3[0] not in a6, a3[1] not in a6, a3[2] not
> in a6"
>                 elif a3[2] in a6:
>                     print "a3[0] not in a6, a3[1] not in a6, a3[2] in
> a6"
>                 else:
>                     print "NONE3.1"
>             elif a3[1] in a6:
>                 if a3[2] not in a6:
>                     print "a3[0] not in a6, a3[1] in a6, a3[2] not in
> a6"
>                 elif a3[2] in a6:
>                     print "a3[0] not in a6,a3[1] in a6, a3[2] in a6"
>                 else:
>                     print "NONE3.2"
>             else:
>                 print "NONE3.3"
>         elif a3[0] in a6:
>             if a3[1] not in a6:
>                 if a3[2] not in a6:
>                     print "a3[0] in a6, a3[1] not in a6, a3[2] not in
> a6"
>                 elif a3[2] in a6:
>                     print "a3[0] in a6, a3[1] not in a6, a3[2] in a6"
>                 else:
>                     print "NONE3.4"
>             elif a3[1] in a6:
>                if a3[2] not in a6:
>                    print "a3[0] in a6, a3[1] in a6, a3[2] not in a6"
>                elif a3[2] in a6:
>                    print "a3[0] in a6, a3[1] in a6, a3[2] in a6"
>                else:
>                    print "NONE3.5"
>             else:
>                 print "NONE3.6"
>         else:
>             print "NONE3.7"
>   
I guess you're looking for one or several of three strings inside a 
longer string. The algorithm is quadratic, no wonder your software 
doesn't respond for larger datasets. Someone spoke about Aho-Corasick 
recently on this list, you should defenitely consider it.

Moreover, the least we could say is that it doesn't loks pythonic, do 
you think the following does the same thing as your snip?

L = []
for i, x in enumerate(a3):
    if x in a6:
        L.append('a3[%d] in a6' % i)
    else:
        L.append('a3[%d] not in a6' % i)
print ', '.join(L)


RB



More information about the Python-list mailing list