本文实例讲述了Python实现的字典值比较功能。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#coding=utf8
import logging
import os
from Lib.DealCsv import ExceptPropertyDic
from 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 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' : '发现_推荐' ,
'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程序设计有所帮助。
原文链接:http://blog.csdn.net/henni_719/article/details/77507697