How to improve this code?

O

Oltmans

Hello,

Is there someway I can improve the following code(pythonically)?
(Copying from IDLE)
match=[1,2,3,4,5]

def elementsPresent(aList):
result=False
if not aList:
return False
for e in aList:
if e in match:
result=True
else:
result = False
return result
elementsPresent([6,7,8,9,5]) # should return True because 5 is
present in list named match.

Is there somehow I can improve code in elementsPresent()? I'm not a
very good programmer but I sense that idea of using a variable named
'result' inside elementsPresent() doesn't sound very good. Any ideas
will be highly appreciated.

Thanks,
Oltmans
 
D

Diez B. Roggisch

Oltmans said:
Hello,

Is there someway I can improve the following code(pythonically)?
(Copying from IDLE)
match=[1,2,3,4,5]

def elementsPresent(aList):
result=False
if not aList:
return False
for e in aList:
if e in match:
result=True
else:
result = False
return result
elementsPresent([6,7,8,9,5]) # should return True because 5 is
present in list named match.

Is there somehow I can improve code in elementsPresent()? I'm not a
very good programmer but I sense that idea of using a variable named
'result' inside elementsPresent() doesn't sound very good. Any ideas
will be highly appreciated.

1) don't use a global variable for your function. Pass both parameters
2) make the lookup O(1) instead O(n) for you match by using a set
3) stop when something is found
4) Unless you want the code to be working with things that are not a
list, but False, the first "if" is superflous


def elementsPresent(aList, match):
match = set(match)
for item in aList:
if item in match:
return True
return False


It might actually be that turning both lists to sets & checking if these
overlap is faster because it's in pure C.


Diez
 
A

André

Oltmans schrieb:


Is there someway I can improve the following code(pythonically)?
(Copying from IDLE)
match=[1,2,3,4,5]
def elementsPresent(aList):
   result=False
   if not aList:
           return False
   for e in aList:
           if e in match:
                   result=True
           else:
                   result = False
   return result
 elementsPresent([6,7,8,9,5]) # should return True because 5 is
present in list named match.
Is there somehow I can improve code in elementsPresent()? I'm not a
very good programmer but I sense that idea of using a variable named
'result' inside elementsPresent() doesn't sound very good. Any ideas
will be highly appreciated.

1) don't use a global variable for your function. Pass both parameters
2) make the lookup O(1) instead O(n) for you match by using a set
3) stop when something is found
4) Unless you want the code to be working with things that are not a
list, but False, the first "if" is superflous

def elementsPresent(aList, match):
     match = set(match)
     for item in aList:
         if item in match:
             return True
     return False

It might actually be that turning both lists to sets & checking if these
overlap is faster because it's in pure C.

Diez

Here's an example using sets:
.... if set(list_1).intersection(set(list_2)):
.... return True
.... return False
....
is_present([1,2,3], [4,5,6]) False
is_present([1,2,3], [0,2,4])
True


André
 
T

Tim Chase

def elementsPresent(aList, match):
match = set(match)
for item in aList:
if item in match:
return True
return False

This could be rewritten in Python2.5+ as

def elementsPresent(aList, match):
match = set(match)
return any(elem in match for elem in aList)

though as suggested both places, the set intersection may be fastest.

-tkc
 
H

Hendrik van Rooyen

match=[1,2,3,4,5]

def elementsPresent(aList):
result=False
if not aList:
return False
for e in aList:
if e in match:
result=True
else:
result = False
return result
elementsPresent([6,7,8,9,5]) # should return True because 5 is
present in list named match.

What are you trying to do?
Your code will only work if the last item in aList is in the match global.
Is this what you want?
or do you want:

(i) a True if All the elements in match are in aList, else False?
(ii) a True if any one or more of the members of match are in aList?
(iii) Something else?

- Hendrik
 
O

Oltmans

(i) a True if All the elements in match are in aList, else False?
(ii) a True if any one or more of the members of match are in aList?
(iii) Something else?


That's a good question because I failed miserably in explaining my
problem clearly. My original question isn't what I'm trying to solve.
My apologies. I will try to explain here clearly. I'm using a 3rd-
party library named Selenium (used for web-automation) and it has a
method named is_element_present(ele) i.e. takes one element and return
true if it finds this element in the page's HTML and returns false
otherwise. Given this, I'm just trying to write a method
are_elements_present(aList) whose job is to return True if and only if
all elements in aList are present in page's HTML. So here is how
are_elements_present() looks like


def are_elements_present(eleLocators):
elePresent=False
if not eleLocators:
return False

for ele in eleLocators:
if selenium.is_element_present(ele):
elePresent=True
else:
elePresent=False
print 'cannot find this element= '+str(ele)
break
return elePresent



Now suppose page HTML contains with these IDs ( ID is an attribute
like <input id="inp1" />) = div1,div2,div3,div4,div5,inp1,inp2
and if I call the above method this way are_elements_present
([div1,div2,inp1,inp2]) then it should return True. If I call like
are_elements_present([div1,div2,div10,inp1]) it should return False.
So I hope I've explained myself. Now all I'm looking for is to write
are_elements_presents() in a more Pythonic way. So please let me know
if I can write are_elements_present() in more smart/shorter way.

Thanks a lot for your help, in advance.
Best regards,
Oltmans
 
T

Tim Golden

Sol said:
def are_elements_present(sourceList, searchList): for e in searchList:
if e not in sourceList:
return False
return True


Using set:
def are_elements_present(sourceList, searchList):
return len(set(sourceList).intersection(set(searchList)) ==
len(searchList)


Unless I'm missing something, (and I didn't bother to
read the original code so I may be) that's a subset test:

set (searchList) <= set (searchList)

TJG
 
T

Tim Golden

Tim said:
Unless I'm missing something, (and I didn't bother to
read the original code so I may be) that's a subset test:

set (searchList) <= set (searchList)

(cough) or, rather:

set (searchList) <= set (sourceList)

TJG
 
D

Dave Angel

Oltmans said:
(i) a True if All the elements in match are in aList, else False?
(ii) a True if any one or more of the members of match are in aList?
(iii) Something else?


That's a good question because I failed miserably in explaining my
problem clearly. My original question isn't what I'm trying to solve.
My apologies. I will try to explain here clearly. I'm using a 3rd-
party library named Selenium (used for web-automation) and it has a
method named is_element_present(ele) i.e. takes one element and return
true if it finds this element in the page's HTML and returns false
otherwise. Given this, I'm just trying to write a method
are_elements_present(aList) whose job is to return True if and only if
all elements in aList are present in page's HTML. So here is how
are_elements_present() looks like


def are_elements_present(eleLocators):
elePresent=False
if not eleLocators:
return False

for ele in eleLocators:
if selenium.is_element_present(ele):
elePresent=True
else:
elePresent=False
print 'cannot find this element= '+str(ele)
break
return elePresent



Now suppose page HTML contains with these IDs ( ID is an attribute
like <input id="inp1" />) = div1,div2,div3,div4,div5,inp1,inp2
and if I call the above method this way are_elements_present
([div1,div2,inp1,inp2]) then it should return True. If I call like
are_elements_present([div1,div2,div10,inp1]) it should return False.
So I hope I've explained myself. Now all I'm looking for is to write
are_elements_presents() in a more Pythonic way. So please let me know
if I can write are_elements_present() in more smart/shorter way.

Thanks a lot for your help, in advance.
Best regards,
Oltmans
def are_all_elements_present(elems):
if not elems:
return False #because you want it "backward"
return all( selenium.is_element_present(elem) for elem in elems )

The extra check is there because you're specifying that an empty list
should return False, and all() returns True if no matches.

DaveA
 
A

Andreas Waldenburger

Here's an example using sets:

... if set(list_1).intersection(set(list_2)):
... return True
... return False
...

Not that it matters, but I'd probably write:

def is_present(test, match):
return bool(set(test) & set(match))

/W
 
S

Simon Forman

Hello,

Is there someway I can improve the following code(pythonically)?
(Copying from IDLE)
match=[1,2,3,4,5]

def elementsPresent(aList):
        result=False
        if not aList:
                return False
        for e in aList:
                if e in match:
                        result=True
                else:
                        result = False
        return result
 elementsPresent([6,7,8,9,5]) # should return True because 5 is
present in list named match.

Is there somehow I can improve code in elementsPresent()? I'm not a
very good programmer but I sense that idea of using a variable named
'result' inside elementsPresent() doesn't sound very good. Any ideas
will be highly appreciated.

Thanks,
Oltmans

It's not really relevant to this particular task (since you want to
stop after finding an 'e' in match) but whenever you're doing
something like this:

if e in match:
result = True
else:
result = False

You can just assign the result of the boolean expression directly to
your result:

result = e in match


Regards,
~Simon
 
S

Sergey Simonenko

elements_present = lambda seq, match: any(((x in match) for x in seq))

Hello,

Is there someway I can improve the following code(pythonically)?
(Copying from IDLE)
match=[1,2,3,4,5]

def elementsPresent(aList):
result=False
if not aList:
return False
for e in aList:
if e in match:
result=True
else:
result = False
return result
elementsPresent([6,7,8,9,5]) # should return True because 5 is
present in list named match.

Is there somehow I can improve code in elementsPresent()? I'm not a
very good programmer but I sense that idea of using a variable named
'result' inside elementsPresent() doesn't sound very good. Any ideas
will be highly appreciated.

Thanks,
Oltmans
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top