map的用法 ——.我们来分析map在python的源码 class map(object): """ map(func, *iterables) --> map object Make an iterator that computes the function using arguments from 利用来自可迭代的每个参数,来计算迭代的函数,当短的可迭代的参数耗尽,程序停止 each of the iterables. Stops when the shorte…
开一篇文章,记录关于Python有意思的用法,不断更新 1.Python树的遍历 def sum(t): tmp=0 for k in t: if not isinstance(k,list): tmp+=k else: tmp+=sum(k) return tmp if __name__=='__main__': x=[1,[2,[3,4,5,[6,7,[8,9]]]]] t=sum(x) print(t)…
Python Enum 枚举 用法汇总 import os import sys if sys.version_info.major + sys.version_info.minor * 0.1 < 3.4: from enum34 import Enum else: from enum import Enum class MyColorEnum(Enum): red = 1 red_alias = 1 blue = 2 green = 'green' MyColorEnum.red.name…
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setdefault() #D.get(k,d) #说明:k在D中,则返回 D[K],如果k不在D中,则返回d值 #D.get(k,d), also set D[k]=d if k not in D ''' >>> help(dict.setdefault) Help on built-in function setdefault: setdefault(...) D.set…
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法capitalize() #capitalize() #说明:将字符串的第一个字母变成大写,其他字母变小写. ''' capitalize(...) S.capitalize() -> string Return a copy of the string S with only its first character capitalized. ''' #案例 str='xiaoden…