所需模块
from collections.abc import Iterable
函数
def pingpu(x):
def flat_gen(x):
def iselement(e):
return not (isinstance(e, Iterable) and not isinstance(e, str))
for el in x:
if iselement(el):
yield el
else:
yield from flat_gen(el)
resource = flat_gen(x)
page = []
for p in resource:
page.append(p)
return page
def list_except(ded, son):
''' 求两个列表的差集 '''
ded = pingpu(ded)
son = pingpu(son)
differ = list(set(ded).difference(set(son)))
return differ
def list_intersect(mom,ded):
''' 求交集 '''
ded = pingpu(ded)
mom = pingpu(mom)
intersect = list(set(ded).intersection(set(mom)))
return intersect
def list_union(mom,ded):
''' 求并集 '''
ded = pingpu(ded)
mom = pingpu(mom)
union = list(set(ded).union(set(mom)))
return union
运用:
求差集
from Normal_Defs.list_def import list_except
lst = ('产品', '应用负责人', '测试', ['业务域运维负责人', '业务域DBA负责人'])
lst2 = {'业务域运维负责人': 'ss','业务域DBA负责人': 'ss'}
diff = list_except(lst, lst2)
print(diff)
['产品', '应用负责人', '测试']
求交集
from Normal_Defs.list_def import list_except,list_intersect
lst = ('产品', '应用负责人', '测试', ['业务域运维负责人', '业务域DBA负责人'])
lst2 = {'业务域运维负责人': 'ss','业务域DBA负责人': 'ss'}
diff = list_intersect(lst, lst2)
print(diff)
['业务域运维负责人', '业务域DBA负责人']
求并集
from Normal_Defs.list_def import list_except, list_intersect, list_union
lst = ('产品', '应用负责人', '测试', ['业务域运维负责人', '业务域DBA负责人'])
lst2 = {'业务域运维负责人': 'ss','业务域DBA负责人': 'ss'}
diff = list_union(lst, lst2)
print(diff)
['业务域DBA负责人', '业务域运维负责人', '测试', '产品', '应用负责人']