Python實(shí)現(xiàn)的字典值比較功能示例
本文實(shí)例講述了Python實(shí)現(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ù)包含兩個(gè): #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' : '焦點(diǎn)圖' , 'srcTitle' : '焦點(diǎn)圖標(biāo)題' , 'focusId' : '焦點(diǎn)圖ID' } ExpecDic={ 'itemId ': 'XX', 'item' : 'track' , 'serviceId' : 'pageview' , 'srcSubModule' : '聲音條' , 'srcPosition' : 'XX', 'srcPage' : '發(fā)現(xiàn)_推薦|猜你喜歡|訂閱' , 'srcPageId' : 'XX' , 'srcModule' : '焦點(diǎn)圖' , 'srcTitle' : '焦點(diǎn)圖標(biāo)題' , 'focusId' : '焦點(diǎn)圖ID' } if __name__=="__main__": print "腳本之家測(cè)試結(jié)果:" if CmpValue(propsDic, ExpecDic): print "Equel" else: print "not equel"
運(yùn)行結(jié)果:
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字典操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python urlopen()和urlretrieve()用法解析
這篇文章主要介紹了Python urlopen()和urlretrieve()用法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01python腳本內(nèi)運(yùn)行l(wèi)inux命令的方法
這篇文章主要介紹了python腳本內(nèi)運(yùn)行l(wèi)inux命令的方法,實(shí)例分析了Python基于subprocess模塊操作Linux命令的相關(guān)技巧,需要的朋友可以參考下2015-07-07Python使用Beautiful Soup爬取豆瓣音樂排行榜過程解析
這篇文章主要介紹了Python使用Beautiful Soup爬取網(wǎng)頁過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Python sql注入 過濾字符串的非法字符實(shí)例
這篇文章主要介紹了Python sql注入 過濾字符串的非法字符實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python實(shí)現(xiàn)字典的遍歷與排序功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)字典的遍歷與排序功能,結(jié)合實(shí)例形式分析了Python字典的遍歷與排序相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下2017-12-12Python實(shí)現(xiàn)圖像隨機(jī)添加椒鹽噪聲和高斯噪聲
圖像噪聲是指存在于圖像數(shù)據(jù)中的不必要的或多余的干擾信息。在噪聲的概念中,通常采用信噪比(Signal-Noise?Rate,?SNR)衡量圖像噪聲。本文將利用Python實(shí)現(xiàn)對(duì)圖像隨機(jī)添加椒鹽噪聲和高斯噪聲,感興趣的可以了解一下2022-09-09