Python学习路程day3

set集合

​set是一个无序且不重复的元素集合,访问速度快,天生解决重复问题

s1 = set()

s1.add('luo')​

s2 = set (['luo','wu','wei','ling'])           

​s3 = s2.difference(['luo','wu'])            #s2中数据拿来逐个匹配,不同的被赋值给s3

​s2.difference_update(['luo','wu'])       #s2数据逐个匹配,修改s2

difference:不修改原来集合,创建一个新的集合

difference_update:修改原集合,不需要赋值,如赋值,返回值为None

s2.pop()         #随机删除一个数据​,没有返回值

s2.remove('luo')     #移除数据luo,可以赋值给变量​

例:​

# 数据库中原有

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())​

交集:​把old和new相同的元素取出来赋值给update_set

update_set = old.intersection(new)

差集:​从old中拿出和update_set相同的数据,old中不同的数值赋值给delete

delete_set = old.difference(update_set)​​

add_set = new.difference(update_set)​

对称差:把new和update_set不同的都拿出来

delete_set = old.symmetric_difference(update_set)​​

add_set = new.symmetric_difference(update_set)​

collections​

​一、计数器(counter)

Counter是对字典类型的补充,用于追踪值的出现次数

ps:具备字典的所有功能 + 自己的功能

import collections​

= collections​.Counter('abcdeabcdabcaba')k

print c

输出:Counter({'a'5'b'4'c'3'd'2'e'1})

c.update(['eric','11','11'])​ #添加元素到变量c

c.subtract(['11','eric'])​   #删除变量c里面的这些元素

​for k in c.elements():                 #elements是元素,取出里面所有元素

    print (k)

for k,v in c.items():                    #取key和values

    print (k,v)​

二、有序字典​

函数​

def 函数名(参数):     

    ...

    函数体

    ...

函数的定义主要有如下要点:

  • def:表示函数的关键字
    • 函数名:函数的名称,日后根据函数名调用函数
    • 函数体:函数中进行一系列的逻辑计算,如:发送邮件、计算出 [11,22,38,888,2]中的最                大数等...
    • 参数:为函数体提供数据
    • 返回值:当函数执行完毕后,可以给调用者返回数据。return是关键字,默认为None,return以下的代码是不会执行的

def show():

  print('a')   

  return 123          #返回值是123,下面的print('b')不会执行

  print('b')

f = show()

print (f​)             #会有'a'和123

函数的普通参数

 user为形式参数,最后面的mail()括号的为实际参数

#!/usr/bin/env python
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
def mail(user):
    ret = True
    try:
        msg = MIMEText('邮件内容','plain','utf-8')
        msg['From'] = formataddr(['武沛齐','[email protected]'])
        msg['To'] = formataddr(['走人','[email protected]'])
        msg['Subject'] = '主题'
        server = smtplib.SMTP("smtp.126.com",25)
        server.login("[email protected]","WW.3945.59")
        server.sendmail("[email protected]",[user,],msg.as_string())
        server.quit()
    except Exception:
        ret = False
    return ret
ret = mail('[email protected]')          #输入要发送的用户名
if ret:
    print("发送成功")
else:
    print("发送失败")

多个参数

#!/usr/bin/env python

#一个参数
def show(arg):
    print (arg)
show("kkkkkk")

#两个参数
def show(arg,xxx):
    print (arg,xxx)
show("kkkkkk","777")
View Code

函数的默认参数:

默认参数必须放在最后,如果给a2赋值,那么a2的默认值会被覆盖

def show(a1,a2=999):
    print (a1,a2)
show(111)
show(111,888)

指定参数:

def show(a1,a2):
    print (a1,a2)
show (a2=123,a1=999)

函数的动态参数:

实际参数可以是“元组”,“列表”,"字典"

一个"*"默认是”元组"

两个"*"默认是”字典“

def show(*arg):
    print (arg,type(arg))
show(1,2,3,4,5,6)
>>>(1,2,3,4,5,6)<class 'tuple'>

def show(**arg):
print (arg,type(arg))
show (n1=78,uu=123,bb=999)
>>>{'bb':999,'n1':78,'uu':123}<class'dict'>

还可以“*”,“**”组合使用,"**"必须放在“*”后面

def show(*args,**kwargs):
    print (args,type(args))
    print (kwargs,type(kwargs))
show (11,22,33,44,n1=88,luo='stupid')

如果要把一个字典和一个列表分别放进(*arg和**kwargs),就分别在字典和列表的变量加对应的*和**

 

你可能感兴趣的:(Python学习路程day3)