首页 > 编程 > Python > 正文

Python实现的字典值比较功能示例

2019-11-25 15:23:51
字体:
来源:转载
供稿:网友

本文实例讲述了Python实现的字典值比较功能。分享给大家供大家参考,具体如下:

#coding=utf8import loggingimport osfrom Lib.DealCsv import ExceptPropertyDicfrom wxPython._wx import false'''''用于json数据的比较,包含属性、属性值的比较。'''#用于比较字符串、列表PATH=lambda p:os.path.abspath(os.path.join(  os.path.dirname(__file__), p))logging.basicConfig(level=logging.DEBUG,        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',        datefmt='%a, %d %b %Y %H:%M:%S',        filename=PATH('../Log/judgeProps.log'),        filemode='w')def CmpObj( reaResultl,exceptResult):  try:    if len(reaResultl)==len(exceptResult):      if cmp(reaResultl,exceptResult)==0:        return True      else:        return False    else:      return False  except Exception,e:    print e#参数包含两个:#containVar:查找包含的字符#stringVar:所要查找的字符串def containVarInString(containVar,stringVar):  try:    if isinstance(stringVar, str):      if containVar in stringVar:        return True      else:        return False    else:      return False  except Exception,e:    print edef CmpValue(propsDic,exceptDic):  try:    containSeparatorList=[val for var in exceptDic.values() if containVarInString("|",var) for val in var.split("|")]    notContainSeparatorList=[var for var in exceptDic.values() if not containVarInString("|",var)]    exceptValueList=notContainSeparatorList+containSeparatorList    FalseBool=list(set([False for var in propsDic.values() if var not in exceptValueList ]))    if len(FalseBool):      return False    else:      return True  except Exception,e:    print epropsDic={  'itemId ': 'XX',  'item' : 'track' ,  'serviceId' : 'pageview' ,  'srcSubModule' : '声音条' ,  'srcPosition' : 'XX',  'srcPage' : '发现_推荐' ,  'srcPageId' : 'XX' ,  'srcModule' : '焦点图' ,  'srcTitle' : '焦点图标题' ,  'focusId' : '焦点图ID'  }ExpecDic={  'itemId ': 'XX',  'item' : 'track' ,  'serviceId' : 'pageview' ,  'srcSubModule' : '声音条' ,  'srcPosition' : 'XX',  'srcPage' : '发现_推荐|猜你喜欢|订阅' ,  'srcPageId' : 'XX' ,  'srcModule' : '焦点图' ,  'srcTitle' : '焦点图标题' ,  'focusId' : '焦点图ID'  }if __name__=="__main__":  print "武林网测试结果:"  if CmpValue(propsDic, ExpecDic):    print "Equel"  else:    print "not equel"

运行结果:

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python字典操作技巧汇总》、《Python数据结构与算法教程》、《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表