Filter Function within Python

…I do not know what to write for this one…


 l_01=[1,2,3,4,5,6,7]
l_02=["A",'B','c',"d","","8"]
def is_odd(n):
	return n%2==1
print(list(filter(is_odd,l_01)))

def not_empty(s):
	return s and s.strip()
print(list(filter(not_empty,l_02)))

def odd_i():
	n=1
	while True:
		n=n+2
		yield n
def cannot_divid(n):
	return lambda x:x%n>0 # return a function here
def primes():
	yield 2
	itlist=odd_i()
	while True:
		n=next(itlist)
		yield n
		itlist=filter(cannot_divid(n),itlist) 
		# write cannot_divid(n) to include(n) since it is a function(not a result) 
		# returned here and argument of this function is in need to continue 

for n in primes():
    if n < 10:
        print(n)
    else:
        break

l_03=["111","123","90909","aba","cc0cc","sda"]
def smooth(n):
		return str(n) == str(n)[::-1]
print(list(filter(smooth,l_03)))

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s