Python實現(xiàn)的字典值比較功能示例
更新時間:2018年01月08日 11:23:58 作者:壞蛋是我
這篇文章主要介紹了Python實現(xiàn)的字典值比較功能,可實現(xiàn)針對字典格式數(shù)據(jù)的判斷、比較功能,涉及Python字典格式數(shù)據(jù)的遍歷、判斷等相關操作技巧,需要的朋友可以參考下
本文實例講述了Python實現(xiàn)的字典值比較功能。分享給大家供大家參考,具體如下:
#coding=utf8 import logging import os from Lib.DealCsv import ExceptPropertyDic from wxPython._wx import false ''''' 用于json數(shù)據(jù)的比較,包含屬性、屬性值的比較。 ''' #用于比較字符串、列表 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 #參數(shù)包含兩個: #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 e def 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 e propsDic={ 'itemId ': 'XX', 'item' : 'track' , 'serviceId' : 'pageview' , 'srcSubModule' : '聲音條' , 'srcPosition' : 'XX', 'srcPage' : '發(fā)現(xiàn)_推薦' , 'srcPageId' : 'XX' , 'srcModule' : '焦點圖' , 'srcTitle' : '焦點圖標題' , 'focusId' : '焦點圖ID' } ExpecDic={ 'itemId ': 'XX', 'item' : 'track' , 'serviceId' : 'pageview' , 'srcSubModule' : '聲音條' , 'srcPosition' : 'XX', 'srcPage' : '發(fā)現(xiàn)_推薦|猜你喜歡|訂閱' , 'srcPageId' : 'XX' , 'srcModule' : '焦點圖' , 'srcTitle' : '焦點圖標題' , 'focusId' : '焦點圖ID' } if __name__=="__main__": print "腳本之家測試結果:" if CmpValue(propsDic, ExpecDic): print "Equel" else: print "not equel"
運行結果:
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python字典操作技巧匯總》、《Python數(shù)據(jù)結構與算法教程》、《Python加密解密算法與技巧總結》、《Python編碼操作技巧總結》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python urlopen()和urlretrieve()用法解析
這篇文章主要介紹了Python urlopen()和urlretrieve()用法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01Python使用Beautiful Soup爬取豆瓣音樂排行榜過程解析
這篇文章主要介紹了Python使用Beautiful Soup爬取網頁過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08