Python@TW: 會眾| IRC| Planet| 郵件列表| 聯絡我們

自 Python 2.5 以後,內建 all() 與 any() 可用來判別选代子是否存有任何值為真或假。

以碼演義:

   1 >>> GoodSoulsComeToRest = ['Strangefolk', ('Monkey', 'Happyfolk') ]
   2 >>> all( GoodSoulsComeToRest )
   3 True
   4 >>> GoodSoulsComeToRest = [ 0, ('Monkey', 'as 0 appear in here...') ]
   5 >>> all( GoodSoulsComeToRest )
   6 False
   7 >>> any( GoodSoulsComeToRest )           # Monkey strings still there
   8 True
   9 >>> black = 0,0,0,0,0,0
  10 >>> ManInBlack = [man for man in black]  # Just a 0-list clone
  11 >>> any( ManInBlack )
  12 False
  13 >>> all( ManInBlack )                    # Since no any, of course not all
  14 False

amix.dk - Python 2.5: Using any function

The fate of reduce() in Python 3000

Other Language Changes in Python 2.5

Python/Cookbook/All&Any (上次是 localhost 在 2009-04-01 04:14:22 編輯的)