Giter Club home page Giter Club logo

my-python-wiki's People

Watchers

 avatar  avatar

my-python-wiki's Issues

python any() 和 all() 函数

内建函数 all()

all(...)
   all(iterable) -> bool
 Return True if bool(x) is True for all values x in the iterable.
 If the iterable is empty, return True.

myall()相当于 内置的 all() 函数
如果 iterable 为空 或者 iterate 内全部元素为 True(即非空) 返回 True
否则,如果 iterable 内部有一个为 False (相当于c语言中的 NULL ),返回 Flase

 def myall(iterable):
    for element in iterable:
     	if not element:
		return False
return True

内建函数 any()

any(...)
    any(iterable) -> bool
    Return True if bool(x) is True for any x in the iterable.
    If the iterable is empty, return False.	   

myany()相当于 内置的 any() 函数
如果,如果 iterable 内任一个元素不为假("",0,False),返回 True
否则,如果 iterable 为空 或者 iterable 内全部元素为假 ,返回 False

def myany(iterable):
   for element in iterable:
    	   if element :
		return True
return False

测试文件

 if __name__ == "__main__":
    no_null = ['a','b','c','d']
    all_null = [0,False,'']    
    is_null =[]
    one_null = ['a','','c','d']
   one_sz_zero = ['a','0','c','d']
   one_num_zero = ['a',0,'c','d']
	   print "any"
   print ""
   print any(no_null) 		# True
   print any(all_null)		# False
   print any(is_null)		# False
   print any(one_null) 	# True
   print any(one_sz_zero) 	# True
   print any(one_num_zero) # True
   print ""
   print "myany"
   print ""
   print myany(no_null) 		# False
   print myany(all_null)		# True
   	   print myany(is_null)		# True
   print myany(one_null) 	    # False
  print myany(one_sz_zero) 	# False
  print myany(one_num_zero)   # False

  print "all"
  print ""
  print all(no_null) 		# True
  print all(all_null)		# False
  print all(is_null)		# True
  print all(one_null) 	# False
  print all(one_sz_zero) 	# True
  print all(one_num_zero) # False
  print ""
  print "myall"
  print ""
  print myall(no_null) 		# True
  print myall(all_null)		# False
  print myall(is_null)		# True
  print myall(one_null) 	    # False
  print myall(one_sz_zero) 	# True

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.