1、練習:html
#!usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Administrator
@file: dictionary.py
@time: 2017/11/19
"""
'''
有以下集合[11,22,33,44,55,66,77,88,99,110,121,132,143,154,165,176,187],將全部大於66的值保存在字典的第一個key中,將小於66的值保存在第二個key的值中
即:{'k1':大於66,'k2':小於66}
'''
######方法一
#dic = {}
#all_list = [11,22,33,44,55,66,77,88,99,110,121,132,143,154,165,176,187]
#for i in all_list:
# if i > 66:
# if "k1" in dic.keys():
# dic["k1"].append(i)
# else:
# dic['k1'] = [i,]
# else:
# if "k2" in dic.keys():
# dic["k2"].append(i)
# else:
# dic['k2'] = [i,]
#print(dic['k1'])
#print(dic['k2'])
#方法2、
all_list = [11,22,33,44,55,66,77,88,99,110,121,132,143,154,165,176,187]
dic = {'k1':[],'k2':[]}
for item in all_list:
if item>66:
dic["k1"].append(item)
else:
dic["k2"].append(item)
print(dic['k1'])
print(dic['k2'])
set集合:
python
#!usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Administrator
@file: set.py
@time: 2017/11/20
"""
#爬蟲
s1 = set()
s1.add("alex")
print(s1)
s1.add('alex')
print(s1)
返回結果:數據庫
{'alex'}api
{'alex'}app
#訪問速度快ide
#天生解決了重複問題函數
clearui
copyspa
過濾重複功能:3d
#!usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Administrator
@file: set.py
@time: 2017/11/20
"""
#爬蟲
s2 = set(["alex","eric","tony","alex"])
print(s2)
返回結果:
{'eric', 'alex', 'tony'}
difference
#!usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Administrator
@file: set.py
@time: 2017/11/20
"""
#爬蟲
s2 = set(["alex","eric","tony","alex"])
print(s2)
s3 = s2.difference(["alex","eric"])
print(s3)
返回結果:
{'tony', 'eric', 'alex'}
{'tony'}
difference
difference_update
#!usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Administrator
@file: set.py
@time: 2017/11/20
"""
#爬蟲
#去除重複的集合
s2 = set(["alex","eric","tony","alex"])
print(s2)
#與s2不相同的部分
s3 = s2.difference(["alex","eric"])
print(s3)
#刪除當前set中的全部包含在參數集合裏的
s4 = s2.difference_update(["alex","eric"])
print(s4)
返回結果:
{'tony', 'eric', 'alex'}
{'tony'}
None
intersection 取交集
isdisjoint 若是沒有交集返回true
issubset 是不是子集
issuperset 是不是父集
pop 移除
remove 移除
symmetric_difference_update 差集
union 並集
update 更新
# 數據庫中原有
old_dict
=
{
"#1"
:{
'hostname'
:c1,
'cpu_count'
:
2
,
'mem_capicity'
:
80
},
"#2"
:{
'hostname'
:c1,
'cpu_count'
:
2
,
'mem_capicity'
:
80
}
"#3"
:{
'hostname'
:c1,
'cpu_count'
:
2
,
'mem_capicity'
:
80
}
}
# cmdb 新彙報的數據
new_dict
=
{
"#1"
:{
'hostname'
:c1,
'cpu_count'
:
2
,
'mem_capicity'
:
800
},
"#3"
:{
'hostname'
:c1,
'cpu_count'
:
2
,
'mem_capicity'
:
80
}
"#4"
:{
'hostname'
:c2,
'cpu_count'
:
2
,
'mem_capicity'
:
80
}
}
交集:要更新的數據
差集:原來,要更新
一、原來沒有 --》 新加入
二、原來有 --》 更新
三、新無,原來有 --》原來刪除
三個列表:
要更新的數據
要刪除
要添加
#!usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Administrator
@file: set.py
@time: 2017/11/20
"""
# 數據庫中原有
old_dict = {
"#1":{ 'hostname':"c1", 'cpu_count': 2, 'mem_capicity': 80 },
"#2":{ 'hostname':"c1", 'cpu_count': 2, 'mem_capicity': 80 },
"#3":{ 'hostname':"c1", 'cpu_count': 2, 'mem_capicity': 80 },
}
# cmdb 新彙報的數據
new_dict = {
"#1":{ 'hostname':"c1", 'cpu_count': 2, 'mem_capicity': 800 },
"#3":{ 'hostname':"c1", 'cpu_count': 2, 'mem_capicity': 80 },
"#4":{ 'hostname':"c2", 'cpu_count': 2, 'mem_capicity': 80 },
}
old = set(old_dict.keys())
new = set(new_dict.keys())
#更新的集合
update_set = old.intersection(new)
#刪除的集合
delete_set = old.symmetric_difference(new)
#添加的集合
add_set = new.symmetric_difference(update_set)
print(update_set)
print(delete_set)
print(add_set)
print(old)
print(new)
返回結果:
{'#1', '#3'}
{'#2', '#4'}
None
{'#1', '#2', '#3'}
{'#4'}
例子:
#!usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Administrator
@file: s1.py
@time: 2017/11/20
"""
s1 = set([11,22,33])
s2 = set([22,44])
ret1 = s1.difference(s2)
ret2 = s1.symmetric_difference(s2)
print(ret1)
print(ret2)
返回結果:
{33, 11}
{33, 11, 44}
http://www.cnblogs.com/wupeiqi/articles/5115190.html
collections 計數器
most_common 最多的次數
element 全部的元素
orderedDict 有序字典
pop
popitem
setdefault 設置默認值
update 更新原來的數據
默認字典:
可命名元祖:建立類
雙向隊列(deque)
http://www.javashuo.com/article/p-kdfuwkyc-bz.html
單向隊列(queue),須要導入queue模塊
https://www.cnblogs.com/zhenwei66/p/6599136.html
郵件模塊:
#!usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Administrator
@file: mail.py
@time: 2017/12/19
"""
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
def mail():
ret = True
try:
msg = MIMEText('how are you,you are a good men','plain','utf-8')
msg["From"] = formataddr(["陳繼鬆",'chenjisong@wtoip.com'])
msg["To"] = formataddr(["406564728@qq.com",'406564728@qq.com'])
msg['Subject'] = "主題"
server = smtplib.SMTP("smtp.wtoip.com",25)
server.login("chenjisong@wtoip.com","**************")
server.sendmail('chenjisong@wtoip.com',["406564728@qq.com",], msg.as_string())
server.quit()
return ret
except Exception:
ret = False
return ret
ret = mail()
if ret:
print('發送成功')
else:
print('發送失敗')
默認參數:
def show(a1,a2=999,a3=333,a4=444,a5=555):
print(a1,a2,a3,a4,a5)
show(111,222,333,444,555)
指定參數:
def show(a1,a2):
print(a1,a2)
show(a2=33334,a1=555556)
動態參數,個數無限制
def show(**arg):
print(arg,type(arg))
show(a1=123,a2=456,a3=789)
返回結果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
{'a1': 123, 'a2': 456, 'a3': 789} <class 'dict'>
動態參數(強強聯合):
def show(*args,**kwargs):
print(args,type(args))
print(kwargs,type(kwargs))
show(11,222,33,44,n1=88,alex="sb")
返回結果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
(11, 222, 33, 44) <class 'tuple'>
{'n1': 88, 'alex': 'sb'} <class 'dict'>
def show(*args,**kwargs):
print(args,type(args))
print(kwargs,type(kwargs))
l = [11,22,33,44]
d = {'n1':88,'alex':'sb'}
show(l,d)
返回結果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
([11, 22, 33, 44], {'n1': 88, 'alex': 'sb'}) <class 'tuple'>
{} <class 'dict'>
若是我想把l放入列表裏面,把d放入字典裏面,則:
def show(*args,**kwargs):
print(args,type(args))
print(kwargs,type(kwargs))
l = [11,22,33,44]
d = {'n1':88,'alex':'sb'}
show(*l,**d)
返回結果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
(11, 22, 33, 44) <class 'tuple'>
{'n1': 88, 'alex': 'sb'} <class 'dict'>
22 python s12 day3 使用動態參數實現字符串格式化
一、
alex = sb的三種方式:
s1 = "{name} is {acter}"
d = {'name':'alex','acter':'sb'}
#result = s1.format(name='alex',acter='sb')
result = s1.format(**d)
print(result)
返回結果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
alex is sb
二、字符串格式化
s1 = "{name} is {acter}"
result = s1.format(name='alex',acter='sb')
print(result)
返回結果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
alex is sb
三、
s1 = "{0} is {1}"
l = ['alex','sb']
result = s1.format('alex','sb')
result = s1.format(*l)
print(result)
返回結果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
alex is sb
23 python s12 day3 Python lambda表達式
def func(a):
a +=1
return a
result = func(4)
print(result)
返回結果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
5
lambda表達式,簡單函數的表示方式:
func = lambda a: a+1
#建立形式參數a
#函數內容a+1,並把結果return
ret = func(99)
print(ret)
返回結果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
100
2四、內置函數:
#絕對值:
a = -100
b = a.__abs__()
print(b)
返回結果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
100
更多詳見:http://www.runoob.com/python/python-built-in-functions.html map的巧用:li = [11,22,33,44]new_li = map(lambda x:x+100,li)l = list(new_li)print(l)返回結果:E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py[111, 122, 133, 144]