>>> import collections >>> # Tally occurrences of words in a list >>> cnt = collections.Counter() >>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']: ... cnt[word] += 1 >>> cnt Counter({'blue': 3,…
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has…
假设可迭代的对象的所有元素所有非空(或者空迭代对象),就返回True.这个函数主要用来推断列表.元组.字典等对象是否有空元素.比方有10000个元素的列表,假设没有提供此函数,须要使用循环来实现.那么计算速度会比較慢.这个函数的等同以下代码的功能: def all(iterable): for element in iterable: if not element: return False return True 样例: #all()函数样例 a = [] b = {1:2, 2:3} c =…
http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html 感谢原作者 30 Python Language Features and Tricks You May Not Know About Posted on Mar 05, 2014 , last modified on Mar 16, 2014 By Sahand Saba 1.1 Unpacking >>>…