1.any () function
Any (iterable)->bool
Returns ture if one of the iterators is ture, or False if Interable=null.
>>> any ([1,0])
True
>>> any ([0,0])
False
>>> any ([])
False
>>> any ([1,0,0])
True
Application: In a binary tree, find the largest element in each layer (leetcode515).
Input: 1 / 3 2 /\ \ 5 3 9 Output: [1, 3,9]
#类节点的定义
Classnode (self):def __init__(self,data) Self.val=Data Self.right=NULL Self.left=NULL
classsolution (object):deflargestvalues (self, root): Maxlist=[] Row=[Root] whileAny (row): Maxlist.append (Max[node.val forNodeinchRow]) Row=[kid forNodeinchRow forKidinchNode.left,node.right)ifKid]returnMaxlist
2.all () function
All (iterable)->bool
When each element in the iterator must be true, returns ture, otherwise false.
>>> all ([1,0])
False
>>> All (["E", 1])
True
3.map ()--built-in high-order functions (functions that can accept function names as parameters-higher-order functions)
python-some useful functions