>>> 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,…
题目来源: https://leetcode.com/problems/longest-common-prefix/ 题意分析: 这道题目是要写一个函数,找出字符串组strs的最长公共前缀子字符串. 题目思路: 这都题目的难度是简单.从字符串头部开始计算,初始化公共前缀子字符串是strs[0],公共子字符串每和下一个字符串得到一个新的最新公共前缀子字符串,直到公共前缀子字符串是空或者比较到了最后一个字符串. 代码(python): class Solution(object): def long…
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…
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution(object):    def lowestCommonAncestor(self, root,…
本函数是从可迭代对象来创建新字典.比方一个元组组成的列表,或者一个字典对象. 样例: #dict() #以键对方式构造字典 d1 = dict(one = 1, two = 2, a = 3) print(d1) #以映射函数方式来构造字典 d2 = dict(zip(['one', 'two', 'three'], [1, 2, 3])) print(d2) #可迭代对象方式来构造字典 d3 = dict([('one', 1), ('two', 2), ('three', 3)]) prin…
本函数是从迭代对象生成集合.集合能够添加或删除元素. 样例: #set() tset = set([1, 2, 3, 3, 4, 5, 6, 6]) print(tset) tset.add(20) print(tset) 结果输出例如以下: {1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 5, 6, 20}…
本函数实现从可迭代对象生成一个元组对象返回.元组对象是一个不可改动的列表对象. 样例: #tuple() print(tuple([1, 2, 3])) print(tuple((1, 2, 3))) print(tuple('abc')) 结果输出例如以下: (1, 2, 3) (1, 2, 3) ('a', 'b', 'c') 蔡军生 QQ:9073204 深圳…
statinfo = os.stat( OneFilePath ) if AllFiles.has_key( statinfo.st_size ): OneKey = AllFiles[ statinfo.st_size ] OneKey.append( OneFilePath ) AllFiles[ statinfo.st_size ] = OneKey else: if statinfo.st_size > MinSize: # print statinfo.st_size AllFiles…
假设可迭代的对象的所有元素所有非空(或者空迭代对象),就返回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 >>>…