Python学习笔记10
1.函数式编程
理论就来自lambda演算,虽然没有学过lisp,一直被其大名震撼。
特性:
函数是以一等公民
可以作为参数
可以作为返回值
具有闭包特性
1.1参数传递方式
- 一般参数传递:值传递,引用传递
- 命名参数传递,使用"参数名=值"的格式,Python内成为关键字参数(keyword argument)
- 默认参数设置
- 可变参数,使用*开头,被解析成为一个元组
- 可变参数,使用**开头,被解析成为一个字典,必须使用关键字参数的方式
- 在调用的时候如何加上*,则会被解成元组或字典
- def func(*args):
- print type(args)
- print args
- func(1,2.3,'true')
- def funcDict(**args):
- print type(args)
- print args
- print args['name']
- funcDict(name='pzdn',age=20)
1.2迭代器Iterator
类似C#的枚举器Enumerator
- 典型标识:next(),it.next()
- lst =range(2)
- it = iter(lst)
- try:
- while True:
- print next(it) # it.next()
- except StopIteration:
- pass
1.3生成器
生成器就是一种迭代器
- 使用yield关键字实现迭代返回
- 和C#的yield是一样的
- 调用next方法实现迭代
- def fibonacci():
- a =b =1
- yield a
- yield b
- while True:
- a,b = b, a+b
- yield b
- for num in fibonacci():
- if num > 100: break
- print num
1.4 enumerate
enumerate类似jquery的$.each
for idx, ele in enumerate(lst):
print idx, ele
1.5lambda
属于匿名函数。
- lambda
args: expression。第一个是关键字,第二个是逗号分隔的参数,冒号之后是表达式块
1.6map
- map,按照func计算之后返回
print map(lambda
x:x**3,range(1,6))
print map(lambda
x:x+x,'abcde')
print map(lambda
x,y:x+y,range(8),range(8))
1.7filter
- 类似Linq的Where扩展方法,选择为true的进行计算
print filter(lambda
x:x%2 != 0 and
x%3 != 0,range(2,20))
1.8reduce
官方解释:
- Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned.
- def reduce(function, iterable, initializer=None):
- it = iter(iterable)
- if initializer is None:
- try:
- initializer = next(it)
- except StopIteration:
- raise TypeError('reduce() of empty sequence with no initial value')
- accum_value = initializer
- for x in iterable:
- accum_value = function(accum_value, x)
- return accum_value
- def statistics(dic,k):
- if not k in dic:
- dic[k] = 1
- else:
- dic[k] +=1
- return dic
- lst = [1,1,2,3,2,3,3,5,6,7,7,6,5,5,5]
- print reduce(statistics,lst,{})
- #提供第三个参数,第一次,初始字典为空,作为statistics的第一个参数,然后遍历lst,作为第二个参数,然后将返回的字典集合作为下一次的第一个参数
- 累加地计算,才能"reduce"减少
print reduce(lambda
x,y:x+y,range(1,101)) #5050
print reduce(lambda
x,y:x+y,range(1,101),20) #5070
1.9闭包
- 类似js.一个作用就是访问作用于的私有变量
另外,"闭包是绑定了外部作用域的变量(但不是全局变量)的函数。即使程序运行至离开外部函数,如果闭包仍然可见,则被绑定的变量仍然有效;每次运行至外部函数,都会重新创建闭包,绑定的变量是不同的,不需要担心在旧的闭包中绑定的变量会被新的值覆盖。"——摘自
2.多线程
2.1简单使用
threading.currentThread()
threading.enumerate()
thread.start_new_thread()
- import thread,threading
- import time
- def print_time(threadName, delay):
- count =0
- while count < 5:
- time.sleep(delay)
- count +=1
- print "%s %s" % (threadName,time.ctime(time.time()))
- print threading.currentThread().getName()
- try:
- thread.start_new_thread(print_time,("T1",4))
- thread.start_new_thread(print_time,("T2",2))
- except:
- print "Error: unable to start thread"
- print threading.enumerate()
- while 1:
- pass
Thread类
thread.exit()
thread.run()
thread.start()
- exitFlag =0
- class myThread(threading.Thread):
- def __init__(self,threadID,name,counter):
- threading.Thread.__init__(self)
- self.threadID = threadID
- self.name = name
- self.counter = counter
- def run(self):
- print "Starting " + self.name
- print_time(self.name,self.counter,5)
- print "Exiting " + self.name
- def print_time(threadName, delay, counter):
- while counter:
- if exitFlag:
- thread.exit()
- time.sleep(delay)
- print "%s: %s" % (threadName, time.ctime(time.time()))
- counter -= 1
- thread1 = myThread(1, "Thread-1", 1)
- thread2 = myThread(2, "Thread-2", 2)
- thread1.start()
- thread2.start()
for t in threads:
t.join()
print
"Exiting Main Thread"
2.2线程同步
threading.Lock().acquire()
threading.Lock().release()
3.Jinja模板
http://erhuabushuo.is-programmer.com/posts/33926.html
强大的模板处理引擎
- 语句块使用:{% 语句 %}
- 取值使用:{{ 值 }}
- 控制流程:
|
{% if title %} {{}} {% else %} {{}} {% endif %} |
- 循环流程:
|
{% for post in posts%} {{}} {% endfor %} |
- 模板继承
{% block content %}{% endblock %}
{% extends "base.html" %}
{% block content %}
{% endblock %}
import
jinja2
template = jinja2.Template('Hello, {{name}}')
print template.render(name="pzdn")
4.简单爬虫框架
urllib:
参考:http://www.cnblogs.com/sysu-blackbear/p/3629420.html
- urllib.urlopen(url[,data[,proxies]])
打开一个url,返回一个文件对象。然后可以进行类似文件对象的操作
- urllib.urlretrieve(url[,filename[,reporthook[,data]]])
将url定位到的html文件下载到你本地的硬盘中。如果不指定filename,则会存为临时文件。
urlretrieve()返回一个二元组(filename,mine_hdrs)
- urllib.urlcleanup()
清除缓存
- urllib.quote(url)和urllib.quote_plus(url)
url编码
- urllib.unquote(url)和urllib.unquote_plus(url)
url解码
- urllib.urlencode(query)
对查询参数编码
|
import import
def downloadPage(url): h = urllib.urlopen(url) return h.read()
def downloadImg(content): pattern = r'src="(.+?\.jpg)" pic_ext' m = re.compile(pattern) urls = re.findall(m, content)
for i, url in urllib.urlretrieve(url, "%s.jpg" % (i, ))
content = downloadPage("http://tieba.baidu.com/p/2460150866") downloadImg(content) |
Python学习笔记10的更多相关文章
- python 学习笔记 10 -- 正則表達式
零.引言 在<Dive into Python>(深入python)中,第七章介绍正則表達式,开篇非常好的引出了正則表達式,以下借用一下:我们都知道python中字符串也有比較简单的方法, ...
- python学习笔记10(Python的内存管理)
用这张图激励一下自己,身边也就只有一位全栈数据工程师!!! 32. Python的内存管理 1. 对象的内存使用 对于整型和短字符串对象,一般内存中只有一个存储,多次引用.其他的长字符串和其他对象 ...
- Python 学习笔记10
念念不忘,必有回响. 今天继续学习Python 类.
- python学习笔记10 ----网络编程
网络编程 网络编程需要知道的概念 网络体系结构就是使用这些用不同媒介连接起来的不同设备和网络系统在不同的应用环境下实现互操作性,并满足各种业务需求的一种粘合剂.网络体系结构解决互质性问题彩是分层方法. ...
- python学习笔记(10):面向对象
一.类和实例 1.类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 2.对象:通过类定义的数据结构实例.对象包括两个数据成员( ...
- Python 学习笔记 - 10.类(Class) 1
定义 Python 的 Class 比较特别,和我们习惯的静态语言类型定义有很大区别. 1. 使用一个名为 __init__ 的方法来完成初始化.2. 使用一个名为 __del__ 的方法来完成类似析 ...
- python学习笔记10(函数一): 函数使用、调用、返回值
一.函数的定义 在某些编程语言当中,函数声明和函数定义是区分开的(在这些编程语言当中函数声明和函数定义可以出现在不同的文件中,比如C语言),但是在Python中,函数声明和函数定义是视为一体的.在Py ...
- Python学习笔记10—几个名词概念
循环(loop),指的是在满足条件的情况下,重复执行同一段代码.比如,while 语句. 迭代(iterate),指的是按照某种顺序逐个访问列表中的每一项.比如,for 语句. 递归(recursio ...
- Python 学习笔记10 函数
函数其实一段带名字的代码段,我们可以根据代码段,重复执行某一段代码段,或者有条件的执行某一段代码段. 将一段代码定义成函数后,我们可以很方便的根据自己的需求,随时调用该代码段.遇到需求变化的时候,只需 ...
随机推荐
- 利用CocoaPods,在项目中导入AFNetworking类库
场景1:利用CocoaPods,在项目中导入AFNetworking类库 AFNetworking类库在GitHub地址是:https://github.com/AFNetworking/AFNetw ...
- elk安装(这个是初级的可以把这个套件安上)
http://udn.yyuap.com/doc/logstash-best-practice-cn/index.html ELK其实并不是一款软件,而是一整套解决方案,是三个开源软件Elastics ...
- ps -C nginx --no-header |wc -l
[root@ok ok]# ps --help|grep C -A all processes -C by command name -V,V show version L list format c ...
- WPF控件
1:内容控件(Content Controls)2:条目控件(Items Controls)3:文本控件(Text Controls)4:范围控件(Range Controls) 一:内容控件 内容控 ...
- 对Cookie和Session的深入理解
session对象 session对象用于存储特定的用户会话所需的信息 . Session对象的引入是为了弥补HTTP协议的不足.HTTP协议本身是无状态的,这与HTTP协议本来的目的是相符的,客户端 ...
- 怎么把MVC的Controller拆分写到别的类库
以为很难…… 其实直接继承Controller 并且按MVC_Controllser规则命名. 然后网站项目引用该项目即可.
- CI重定向:php(codeigniter)中如何重定向
Q: 在保存完数据之后需要重定向,防止数据重复提交. 我使用$this->方法名();跳转,发现不能达到重定向的效果(地址栏没变) 请教高手重定向怎么用 A: $this->load-&g ...
- Arch Linux Installation Guide
Arch Linux Installation Guide timedatectl set-ntp true sed -i '/Score/{/China/!{n;s/^/#/}}' /etc ...
- 在python中使用concurrent.futures实现进程池和线程池
#!/usr/bin/env python # -*- coding: utf-8 -*- import concurrent.futures import time number_list = [1 ...
- C++ 基础 构造函数的使用