您的位置:首页 > 编程语言 > Python开发

python内部函数和常用工具函数

2016-08-22 21:26 459 查看
from itertools import *
'''
itertools中的迭代器工具
'''
def t1():
#排列
for num in permutations('abc', 2):
print num,
def t2():
#组合
for num in combinations('abc', 2):
print num,
def t3():
#包含自身的组合
for num in combinations_with_replacement('abc', 2):
print num,
def t4():
#链式组合循环器
for num in chain([1, 2, 3], [4, 5, 7]):
print num,
def t5():
print map(pow, [1, 2, 3], [1, 2, 3])#返回列表结果
rlt = imap(pow, [1, 2, 3], [1, 3, 3])#返回迭代器
for num in rlt:
print num,
def t6():
#循环器的笛卡尔积
for m, n in product('abc', [1, 2]):
print m, n
def t7():
#过滤循环器
for m in ifilter(lambda x: x > 5, [2, 3, 5, 6, 7]):
print m,
for m in ifilterfalse(lambda x: x > 5, [2, 3, 5, 6, 7]):
print m,
for m in takewhile(lambda x: x<5, [1,4,6,4,1]):
print m,
for m in dropwhile(lambda x: x<5, [1,4,6,4,1]):
print m,
print filter(lambda x: x > 5, [2, 3, 5, 6, 7])

def t8():
#数据分组
def height_class(h):
if h > 180:
return "tall"
elif h < 160:
return "short"
else:
return "middle"

friends = [191, 158, 159, 165, 170, 177, 181, 182, 190]
friends = sorted(friends, key = height_class)
for m, n in groupby(friends, key = height_class):
print(m)
print(list(n))
def t9():
for m in compress('ABCD', [1, 1, 1, 0]):
print m,
t9()


'''
内置函数
'''

def t5():
print map(pow, [1, 2, 3], [1, 2, 3])#返回列表结果
def t7():
print filter(lambda x: x > 5, [2, 3, 5, 6, 7])
def t8():
#需要一个二元操纵函数 reduce( func, seq[, init] )
print reduce(lambda x,y: x+y, [1, 2, 3])
def say(a, b):
print a, b
def t9():
apply(say,("hello", "老王python"))
t9()


#数学运算类
def math_op():
#求绝对值
print abs(-3)
#创建复数
print complex(1,3)
#实现a除以b,然后返回商与余数的元组
print divmod(2, 3)
#类型转换float([x]),int([x[, base]]) ,long([x[, base]])
print int('1010',base=2)
print int('1010')
#x的y次幂 pow(x, y[, z])
print pow(2, 3)
#创建序列 range([start], stop[, step])
print range(10)
#四舍五入 round(x[, n])
print round(3.2)
#对集合求和 sum(iterable[, start])
print sum([1,2,3])
#转换为 8进制 和 16进制
print oct(10)
print hex(10)
#返回整数i对应的ASCII字符
print chr(37)
#将整数x转换为二进制字符串
print bin(x)
#将x转换为Boolean类型 bool([x])
print bool(2)

#集合类操作
def coll_op():
#format(value [, format_spec])
print "I am {0},I like {1}".format('yj','fish')
print eval('0b1010')
#返回给定int类型的unicode chr返回(0~255)字符
print unichr(100)
print chr(100)
#返回可枚举对象,该对象的next()方法将返回一个tuple enumerate(sequence [, start = 0])
for i,n in enumerate(['a','b','c']):print i,n
#生成一个对象的迭代器,第二个参数表示分隔符 iter(o[, sentinel])
for n in iter(['a','b','c']):print n
#返回集合中的最大值、最小值 max(iterable[, args...][key])
print max([2, 3, 5, 6, 7])
print min([2, 3, 5, 6, 7])
#创建字典。集合,set() dict([arg])、list([iterable])、tuple([iterable])
#产生一个不可变的set,frozenset([iterable])
#转换为string类型,str([object])
#队集合排序,sorted(iterable[, cmp[, key[, reverse]]])
print sorted([2, 3, 5, 6, 7], reverse=True)
#类似range,但是返回迭代器,xrange([start], stop[, step])

def logic_judge():
#集合中的元素都为真或为空的时候为真
print all(iterable)
#任意一个为真的时候
print any(iterable)
#cmp(x, y)
print cmp(x, y)

class A():
#初始化时自动调用
def __init__(self,x):
self.x = x
#“()”运算时调用回调方法,可将对象当函数使用
def __call__ (self, key):
return key
class B():
#初始化时自动调用
def __init__(self,x):
self.x = x

def reflect_op():
#是否可调用,类和声明了__call__方法的是可调用的 callable(object)
a = A(1)
b = B(1)
print callable(A),callable(B),callable(a),callable(b)
#classmethod() 表示类方法
#staticmethod 表示静态方法
#dir([object]) 返回变量、方法和定义的类型列表
print dir(a)
#属性操纵 delattr(object, name),getattr(object, name [, defalut]),hasattr(object, name),setattr(object, name, value)
#eval将字符串当成表达式求值 eval(expression [, globals [, locals]])
x=1
print eval('x+1')
a = "{1: 'a', 2: 'b'}"
print eval(a)
#repr函数将变量和表达式转换为字符串表示 repr(object)
#filter(function, iterable)
print filter(lambda x:x>2, [1,2,3,4,5,6])
#对每个元素进行fun,map(function, iterable, ...)
print map(lambda x:pow(x,2), [1,2,3])
#需要一个二元操纵函数 reduce( func, seq[, init] )
print reduce(lambda x,y: x+y, [1, 2, 3])
#常用简单函数 len(s) isinstance(object, classinfo) reload(module) type(object)
#zip([iterable, ...])
for m in zip([1,2,3],['a','b','c']):print m
#slice()返回切片
s = slice(0,5,2)
l = range(10)
print l(s)
'''
compile(source, filename, mode[, flags[, dont_inherit]])
execfile(filename [, globals [, locals]])
globals()
hash(object)
id(object)
issubclass(class, classinfo)
locals()
memoryview(obj)
next(iterator[, default])
object()
property([fget[, fset[, fdel[, doc]]]])
slice()
super(type[, object-or-type])
vars([object])
bytearray([source [, encoding [, errors]]])
'''

reflect_op()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: