$Python常用内置函数典型用法
Python中有许多功能丰富的内置函数,本文基于Python 2.7,就常用的一些函数的典型用法做一些积累,不断更新中。
sorted函数的三种用法
# coding:utf-8
# sorted函数的三种用法
from operator import itemgetter
data1 = [{'aa':22,'bb':11},{'aa':12,'cc':23},{'aa':67,'dd':103}]
data2 = [{'age':18,'name':'Tom'},{'age':10,'name':'Tim'},{'age':30,'name':'John'},{'age':18,'name':'Amy'}]
def sort1():
# 对data1依据'aa'字段值的大小从小打到排序
ret = sorted(data1,key = lambda item:item['aa']) # 注:如果这里的key写'bb'或'cc',会报KeyError,因为这两个属性并不是每个元素都有的
print ret
# 输出:
'''
[{'aa': 12, 'cc': 23}, {'aa': 22, 'bb': 11}, {'aa': 67, 'dd': 103}]
'''
def sort2():
# 对data1依据'aa'字段值的大小从小打到排序
ret = sorted(data1,cmp = lambda x,y:cmp(x['aa'],y['aa']))
print ret
# 输出:
'''
[{'aa': 12, 'cc': 23}, {'aa': 22, 'bb': 11}, {'aa': 67, 'dd': 103}]
'''
def sort3():
# 使用itemgetter对data1依据'aa'字段值的大小从小打到排序
ret = sorted(data1,key = itemgetter('aa'))
print ret
# 输出:
'''
[{'aa': 12, 'cc': 23}, {'aa': 22, 'bb': 11}, {'aa': 67, 'dd': 103}]
'''
def sort4():
# 对data2进行排序,先按照'age'从小到大排序,'age'相同的情况下,再按照'name'排序
ret = sorted(data2,key = itemgetter('age','name'))
print ret
# 输出:
'''
[{'age': 10, 'name': 'Tim'}, {'age': 18, 'name': 'Amy'}, {'age': 18, 'name': 'Tom'}, {'age': 30, 'name': 'John'}]
'''
执行命令行命令的三种方式
# coding:utf-8
# 执行命令行命令的三种方式
import os
import commands
command = 'ls -al /root'
def method1():
'''
方式1
'''
os.system(command)
# 执行结果:返回执行状态码
def method2():
'''
方式2
'''
out1 = os.popen(command)
print out1.read()
# 输出:执行结果字符串
def method3():
'''
方式3
'''
(status,out) = commands.getstatusoutput(command)
# 输出:status是执行状态码,out是执行结果字符串
zip函数的用法
Docstring:
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th element
from each of the argument sequences. The returned list is truncated
in length to the length of the shortest argument sequence.
先来看看zip函数的文档,从文档中可以看出,zip函数接收1个或多个序列作为参数,返回一个由元组组成的列表。
结果列表的第i个元素是seq1~seqn的第i个元素组成的元组。
结果列表的长度等于seq1~seqn中最短的序列的长度。
一段测试代码如下:
# coding:utf-8
def main():
a = '1234'
b = [4,6,7]
print zip()
# 输出:[]
print zip(a)
# 输出:[('1',), ('2',), ('3',), ('4',)]
print zip(a,a)
# 输出:[('1', '1'), ('2', '2'), ('3', '3'), ('4', '4')]
print zip(a,[])
# 输出:[]
print zip(a,b)
# 输出:[('1', 4), ('2', 6), ('3', 7)]
if __name__ == '__main__':
main()
map函数的用法
map函数是一个高阶函数,支持传入一个函数作为参数。先来看它的文档是怎么说的:
Docstring:
map(function, sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).
从map函数的文档中可以看出,该函数的第一个参数为一个函数对象,后面可以跟一个或多个序列,函数的返回值是一个list.
对比zip函数的用法,可以发现其实map函数就是一个增强版的zip函数,与zip函数不同的是,map函数支持传入一个函数参数来处理序列。
如果第一个函数参数不为None,那么返回的结果list的第i个元素,是将该函数作用于每个序列的第i个元素的结果。如果传入的序列的长度不都是相同的,那么结果list的某些元素将会是None.
如果第一个函数参数为None,那么返回的的结果list的第i个元素,是每个序列第i个元素组成的n元组(n为序列的个数),如果每个序列的长度不都是相同的,那么结果list的某些元素将是None.
下面通过一段程序来看map函数的实际用法:
# coding:utf-8
def main():
a = [1,2,3,4]
b = [3,5,9]
c = [8,2,3]
print map(None,a,b,c)
# 输出:[(1, 3, 8), (2, 5, 2), (3, 9, 3), (4, None, None)]
print map(lambda x : x ** 2,a)
# 输出:[1, 4, 9, 16]
# print map(lambda x,y : x + y,a)
# 输出:TypeError <lambda>() takes exactly 2 arguments (1 given)
print map(lambda x,y : x + y,b,c)
# 输出:[11, 7, 12]
# print map(lambda x,y,z : x + y + z,a,b,c)
# 输出:TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
print map(lambda x,y : x + y if x is not None and y is not None else None,a,b)
# 输出:[4, 7, 12, None]
if __name__ == '__main__':
main()
reduce函数的用法
先看函数文档:
Docstring:
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
reduce函数接收三个参数:function,seq,init,其中前两个是必选参数,最后一个为可选参数。
reduce函数做了这样一件事情:从左到右遍历seq,将seq[0]和seq[1]传入函数function进行运算(function为一个接收两个参数的函数),得到一个结果值,然后将这个结果值再和seq[2]传入fucntion进行运算再得到一个新的结果值...以此类推。最终得到一个值,就是该函数的返回值。
如果传入了init,那么init和seq[0]会作为第一次传入funciton的参数,如果seq为空,init也会作为reduce的返回值返回。
用法示例如下:
# coding:utf-8
def main():
lst = [1,2,3]
f = lambda x,y:x*y
print reduce(f,lst)
# 输出:6
print reduce(f,lst,-1)
# 输出:-6
print reduce(f,[],-2)
# 输出:-2
if __name__ == '__main__':
main()
base64编解码
# coding:utf-8
# 测试base64编解码
import base64
def main():
s = '123abc'
# 编码
print base64.b64encode(s)
# 输出:MTIzYWJj
# 解码
print base64.b64decode('MTIzYWJj')
# 输出:123abc
if __name__ == '__main__':
main()
随机推荐
- 第十六篇:Linux系统编程中环境变量的使用
前言 在 UNIX Like 系统中,存有各类系统/应用程序的环境变量,可通过修改之改变系统/应用程序的执行效果:除此之外,用户还可以定义自己的环境变量,供自己写的程序使用. 本文将说明如何在程序中设 ...
- 1853: [Scoi2010]幸运数字[容斥原理]
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 2405 Solved: 887[Submit][Status] ...
- 数组和对象常用API
数组API: 1. forEach 遍历所有元素 var arr = [1,2,3] arr.forEach(function(item,index){ // 遍历数组的所有元素 console.lo ...
- Codeforces Round #513-ABCD
ABC现场做出,涨了八十几分吧.D有点思路不知道怎么实现,赛后看题解发现巨简单,想得太复杂了.蓝瘦. A----http://codeforces.com/contest/1060/problem/A ...
- timepicker php strtotime 8hours
https://jqueryui.com/datepicker/ w timepicker datepicker 日期 时间 选择器 <script src="static/jquer ...
- shell (check return of each line)(PIPESTATUS[@])and sudoer
shell result from cmdline echo $? if 0 then success ;else failure (shell 执行每部返回值,rm -rf 错误,打包不能覆盖) 我 ...
- GITLAB服务基础
1.GITLAB介绍 一个基于GIT的源码托管解决方案基于Ruby on rails开发集成了nginx postgreSQL redis sidekiq等组件 2. 资源 官网:https://ab ...
- 病毒侵袭持续中---hdu3065(AC自动机模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 模板题,没什么好说的... #include<stdio.h> #include&l ...
- python爬虫系列(1)——一个简单的爬虫实例
本文主要实现一个简单的爬虫,目的是从一个百度贴吧页面下载图片. 1. 概述 本文主要实现一个简单的爬虫,目的是从一个百度贴吧页面下载图片.下载图片的步骤如下: 获取网页html文本内容:分析html中 ...
- Incorrect key file for table ' '; try to repair it
场景:为有150W的数据表增加字段时,报错 解决:在my.ini配置临时目录configure tmpdir. Where MySQL Stores Temporary Files