本文實例講述了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程序設計有所幫助。
新聞熱點
疑難解答