python好用的函数或对象
1、ljust、rjust
"hello".ljust(10,"x") #将字符串hello做对齐,并且用字符‘x’补到10个字符 #输出为:helloxxxxx
2、repr
def func():
print("hello") repr(func) # 将函数对象转为字符串 # 输出:function func at 0x00000021DC268,如果是print(func)的话自动回调用这个函数。
3、dir(返回对象的属性(成员),函数在python中也是对象,也有属性)def func(): print("hello")
dir(func) # 返回对象的属性 #输出:['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] import operator
dir(operator)
#['__abs__', '__add__', '__all__', '__and__', '__builtins__', '__cached__', '__concat__', '__contains__', '__delitem__', '__doc__', '__eq__', '__file__', '__floordiv__', '__ge__', '__getitem__', '__gt__', '__iadd__', '__iand__', '__iconcat__', '__ifloordiv__', '__ilshift__', '__imatmul__', '__imod__', '__imul__', '__index__', '__inv__', '__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__', '__itruediv__', '__ixor__', '__le__', '__loader__', '__lshift__', '__lt__', '__matmul__', '__mod__', '__mul__', '__name__', '__ne__', '__neg__', '__not__', '__or__', '__package__', '__pos__', '__pow__', '__rshift__', '__setitem__', '__spec__', '__sub__', '__truediv__', '__xor__', '_abs', 'abs', 'add', 'and_', 'attrgetter', 'concat', 'contains', 'countOf', 'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand', 'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul', 'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift', 'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor',
'le', 'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod', 'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift', 'setitem', 'sub', 'truediv', 'truth', 'xor']
# 其中__mul__、__add__是实现细节,mul,add是调用了这个函数。
4、functools-->reduce 迭代运算
from functools import reduce reduce(lambda a,b:a+b, range(10)) # 计算0~9 数据的和
reduce(lambda a,b:a*b, range(1,10)) #计算1*2*3...*9的积 # reduce(function , iterable_obj),iterable_obj的第一个和第二元素先调用function,得到的结果和第三个元素再调用function,依次类推。
5、operator (C++中也有这些函数对象,替换lambda的作用)
from functools import reduce
import operator reduce(add, range(10)) # 计算0~9 数据的和
reduce(mul, range(1,10)) #计算1*2*3...*9的积
6、operator-->itemgetter(获取可迭代对象的部分数据,多个数据时以元组形式返回)
from operator import itemgetter getter = itemgetter(1) # 构造一个获取迭代对象下标为1的可调用对象。
l = [1,2,3,4,5,6] getter(l) # 输出2 getter = itemgetter(2,5)
getter(l) # 输出(3,6)
getter(l)[0] # 输出3
7、operator-->attrgetter(获取对象的部分属性值,多用于提取namedtuple中的部分数据,多个数据以元组形式返回)
from collections import namedtuple
from operator import attrgetter
motro_data = [
('Tokyo', 'JP', 36.933, (35.68, 139.69)),
('Delhi NCR', 'IN', 21.935, (28.61, 77.20)),
('Mexico City', 'MX', 20.142, (19.43, -99.13)),
('New York-Newark', 'US', 20.104, (40.80, -74.02)),
('Sao Paulo', 'BR', 19.649, (-23.54, -46.63))
] LatLong = namedtuple('LatLong', 'lat long') # 定义一个namedtuple类型
Metropolis = namedtuple('Metropolis', 'name cc pop coord') # 定义一个namedtuple类型
metro_areas = [Metropolis(name, cc, pop, LatLong(lat, long)) for name, cc, pop, (lat, long) in motro_data] # 列表推导拆包生成列表,列表的元素是namedtuple
name_lat = attrgetter('name', 'coord.lat') # 构造一个attrgetter对象
for city in sorted(metro_areas, key=attrgetter('coord.lat')): # sorted根据coord.lat值排序,返回一个排序后的列表,元素还是namedtuple
print(name_lat(city)) # 根据name_lat提取city中的name和coord.lat属性打印
8、functools-->partial(绑定函数部分参数,构造新的函数,跟C++的bind函数类似,C++,用_1,_2这些常量来确定绑定那些参数,python可以通过位置参数和关键字参数确定绑定那些参数)
import functools def func(a,b,c):
print(a,b,c) f = functools.patial(func, 'a', c='c') # 为func函数绑定参数,为a绑定位置实参‘a’,为c绑定关键字实参‘c’
f('hello') # 输出:a hello c # 位置实参只能按照顺序绑定,关键字实参可以绑定任意参数。
python好用的函数或对象的更多相关文章
- Python学习笔记之—— File(文件) 对象常用函数
file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 1.file.close() close() 方法用于关闭一个已打开的文件.关闭后的文件不能再进行读写操作, 否则会触 ...
- 《流畅的Python》第三部分 把函数视作对象 【一等函数】【使用一等函数实现设计模式】【函数装饰器和闭包】
第三部分 第5章 一等函数 一等对象 在运行时创建 能赋值给变量或数据结构中的元素 能作为参数传递给函数 能作为函数的返回结果 在Python中,所有函数都是一等对象 函数是对象 函数本身是 func ...
- python之attrgetter函数对对象排序
# 使用attrgetter函数对对象排序 # attrgetter处理对象,itemgetter处理序列 from operator import attrgetter class user(): ...
- python学习道路(day4note)(函数,形参实参位置参数匿名参数,匿名函数,高阶函数,镶嵌函数)
1.函数 2种编程方法 关键词面向对象:华山派 --->> 类----->class面向过程:少林派 -->> 过程--->def 函数式编程:逍遥派 --> ...
- python学习之路-day6-面向对象
一.面向对象学习 特性 class类 一个类即是对一类拥有相同属性的对象的抽象.蓝图.原型.在类中定义了这些对象的都具备的属性.共同的方法. 面向过程编程和面向对象编程: 面向过程编程:使用一系列的指 ...
- python中常用的函数与库一
1, collections.deque 在python里如果我们用列表作为队列使用也是可以的,只是当从队尾删除或者增加元素的时候是很快的,但是从队首删除或者增加元素则要慢得多,这是因为在队首进行操作 ...
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- python --- Python中的callable 函数
python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...
- python迭代器与iter()函数实例教程
python迭代器与iter()函数实例教程 发布时间:2014-07-16编辑:脚本学堂 本文介绍了python迭代器与iter()函数的用法,Python 的迭代无缝地支持序列对象,而且它还允许程 ...
随机推荐
- vue重置data
Object.assign(this.$data, this.$options.data()) 解析:1.Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象. ...
- Solon 开发,七、自定义注解开发汇总
Solon 开发 一.注入或手动获取配置 二.注入或手动获取Bean 三.构建一个Bean的三种方式 四.Bean 扫描的三种方式 五.切面与环绕拦截 六.提取Bean的函数进行定制开发 七.自定义注 ...
- C#检测外部exe程序弹窗错误,并重启
private void button2_Click(object sender, EventArgs e) { string mainTitle = System.Configuration.Con ...
- k8s-storage-class
1. 简介 StorageClass 为管理员提供了描述存储 "类" 的方法. 通过StorageClass的定义,管理员可以将存储资源定义为某种类别(Class),正如存储设备对 ...
- ManualResetEvent实现线程的暂停与恢复
背景 前些天遇到一个需求,在没有第三方源码的情况下,刷新一个第三方UI,并且拦截到其ajax请求的返回结果.当结果为AVALIABLE的时候,停止刷新并语音提示,否则继续刷新. 分析这个需求,发现需要 ...
- 搭服务器之centos-ipv6源--配置各虚拟机系统的ipv6网络安装源。
在2g内存的台式机里安装了三台虚拟机,跑起来好可以,就是swap用的比较多,图见上一篇随笔.现在平台基本有了,自己笔记本算总控,实验室台式机跑着4台机器(一实三虚),加上一台服务器,可以做很多事情了, ...
- golang中的pair
package main import "fmt" type Reader interface { ReadBook() } type Writer interface { Wri ...
- 访问静态资源有问题(配置url-pattern 用"/")(两种静态资源处理)
发起的请求是由哪些服务器程序处理的 http://localhost:8080/ch05_url_pattern/index.jsp: tomcat(jsp会转为servlet) http://loc ...
- elasticsearch 申请basic证书
如果elasticsearch使用低于6.3版本的,basic证书默认1个月,需要申请,可使用时间为1年. 申请地址为: https://license.elastic.co/registration ...
- numpy 矩阵在作为函数参数传递时的奇怪点
numpy 矩阵在作为函数参数传递时的奇怪点 import numpy as np class simpleNet: def __init__(self): self.W = np.array([1, ...