for loop - Understanding any keyword in Python if statements -
i have 2 python list follows:
>>> clean = ['snat2vip', 'http2https'] >>> ugly = ['/common/snat2vip', '/common/http2https'] i need iterate through each member of clean , see if exists anywhere in ugly. 'exists', mean present whole or substring in element of ugly.
this came with:
>>> item in clean: ... if (word in item word in ugly): ... print "yes" ... else: ... print "no" ... yes yes works expect.however,if add any,the output changes follows:
>>> item in clean: ... if any(word in item word in ugly): ... print "yes" ... else: ... print "no" ... no no what any change output ?
as boolean value of expression (word in item word in ugly) true returns generator object.
you can check using bool built-in function.
bool(word in item word in ugly) so work any built-in function have follow:-
if any(item in word word in ugly): instead of:-
if any(word in item word in ugly): as word string resides in item string.
>>> 'snat2vip' in '/common/snat2vip' true >>> >>> '/common/snat2vip' in 'snat2vip' false
Comments
Post a Comment