一、collection系列
1、计数器(counter)
collection是对字典中重复字符出现的次数,其具备字典的所有功能加自己的功能相对与包含字典。
举例:

#!/usr/bin/env  python
# --*--coding:utf-8 --*--
import collections
c = collections.Counter("sdsasdsa") 
print c

输出结果是:

Counter({'s': 4, 'a': 2, 'd': 2})

如果说提取里面的值就需要用到

b = collections.Counter('aswedswedswedswed')
print b
b.update(c) #把c添加到b里面
print b

输出结果是:

Counter({'s': 4, 'a': 2, 'd': 2})
Counter({'s': 4, 'e': 4, 'd': 4, 'w': 4, 'a': 1})
Counter({'s': 8, 'd': 6, 'e': 4, 'w': 4, 'a': 3})
import collections
c = collections.Counter("sdsasdsa") #打印出后面重复的字符串
print c
print c.most_common(3) #显示前几个
输出结果是:
Counter({'s': 4, 'a': 2, 'd': 2})
[('s', 4), ('a', 2), ('d', 2)]
print sorted(b) #按顺序打印元素
print b

输出结果是:

Counter({'s': 4, 'e': 4, 'd': 4, 'w': 4, 'a': 1})
['a', 'd', 'e', 's', 'w']

2、默认字典(defaultdict)
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66 , 'k2': 小于66}

from collections import defaultdict
values = [11, 22, 33,44,55,66,77,88,99,90]
my_dict = defaultdict(list)
for value in  values:
    if value>66:
         my_dict['k1'].append(value)
    else:
         my_dict['k2'].append(value)
print my_dict

输出结果是:

defaultdict(<type 'list'>, {'k2': [11, 22, 33, 44, 55, 66], 'k1': [77, 88, 99, 90]})

3、有序字典(orderedDict )
记住了字典元素的添加顺序,进行排序

from collections import OrderedDict
a = {'a':1 , 'b':2 , 'c': 3}
print OrderedDict(sorted(a.items(), key=lambda t: t[1]))
print OrderedDict(sorted(a.items(), key=lambda t: len(t[0])))
print OrderedDict(sorted(a.items(), key=lambda t: t[1]))

输出结果是:

OrderedDict([('a', 1), ('b', 2), ('c', 3)])
OrderedDict([('a', 1), ('c', 3), ('b', 2)])
OrderedDict([('a', 1), ('b', 2), ('c', 3)])

4、可命名元组(namedtuple)
根据nametuple可以创建一个包含tuple所有功能以及其他功能的类型,可命名元组

Mytuple = collections.namedtuple('Mytuple',['x', 'y'])    #新建元组
old = Mytuple(1, 2)
print old
new = Mytuple(1,2)
print new

输出结果是:

Mytuple(x=1, y=2)
Mytuple(x=1, y=2)

5、双向队列(deque)
#线程安全的双向队列

q = collections.deque() #加双向队列
q.append(11)    #添加队列
q.append(12)
q.append(13)
print q
print q.popleft() #左边取值
print q.pop() #右边取值

#单向队列,队列FIFO ,栈,弹夹

import Queue    #模块
q = Queue.Queue()
q.put(1) #价值
q.put(2)
q.put(3)
print q.get(1)    #取值

#vars() = 当前模块的所有变量

# print vars()
# {'__builtins__': <module '__builtin__' (built-in)>, '__file__': 'D:/s11day2/s11day2/coll.py', '__package__': None, 'collections': <module 'collections' from 'D:\Python27\Lib\collections.pyc'>, '__name__': '__main__', '__doc__': None}

二、内置函数
内置函数就是,python内部已经定义好的函数
all(值) #当值都是真的时候,返回真,否则返回假
any()   #只要有一个真,全为真,否则为假

>>> li = ['as', 'aa', '']
>>> any(li)
True
>>> all(li)
False
>>> li = ['as', 'aa', 'as']
>>> all(li)
True
>>> any(li)
True
>>> li = ['', '', '']
>>> any(li)
False
for k,v in enumerate(li, 起始值)
    print k,v

三、自定义函数
自己写好的函数,在用的时候调用,不用的时候不执行
#邮件报警

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
def email(message): #收件人可以是一个列表多个参数receiver
    msg = MIMEText(message, 'plain', 'utf-8')
    msg['From'] = formataddr(["发件者姓名",'发件邮箱'])
    msg['To'] = formataddr(["收件者姓名",'收件箱'])
    msg['Subject'] = "主题"
    server = smtplib.SMTP("smtp.qq.com", 25)
    server.login("发件邮箱", "密码")
    server.sendmail('发件邮箱', ['收件箱',], msg.as_string())
    server.quit()

#收件人可以是一个列表多个参数receiver
#在加一个for循环

if __name__ == '__main__':
    cpu = 90
    disk = 100
    ram = 50
    for i in range(1):
        if cpu > 80:
            alert = u"CPU报警"
            email(alert)
        if disk > 120:
            alert = u"disk报警"
            emial(alert)
        if ram > 20:
            alert = u"内存报警"
            email(alert)

四、动态参数

图解:

wKioL1ZGma7A2ROlAAAwGTs9Ucc106.png

>>> def func(*arg):
...     print arg
...
>>> func()
()
>>> func(1)
(1,)
>>> func(1,2)
(1, 2)
>>> func(1,2,3)
(1, 2, 3)
>>> li = (11,22,33,44,55,66)
>>> func(li)
((11, 22, 33, 44, 55, 66),)
>>> func(*li)
(11, 22, 33, 44, 55, 66)

1、接受多个参数
2、内部自动构造元组
3、序列,*,避免内部构造元组
加一个*内部构造是元组,两个**kwargs是构造字典,传入的时候要func(k1=123,k2=124),传入字典

func(**dic)
>>> def func(**kwargs):
...     print kwargs
...
>>> func()
{}
>>> func(123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: func() takes exactly 0 arguments (1 given)
>>> func(k1=123,k2=234)
{'k2': 234, 'k1': 123}
>>> ls = {'k1':233, 'k2':345}
>>> func(**ls)
{'k2': 345, 'k1': 233}

两个动态函数结合