我有两个列表,我喜欢浏览它们以获得匹配和差异 但我不明白为什么我的索引没有到达列表的末尾 有我的代码:
recordedEvents=[u'/app//wakeup', u'/app/hdmi/state', u'/homepage/ws/view', u'/homepage//activate', u'/live//activate', u'/live//zap', u'/live/ctazap/view', u'/live/ppluspminus/zap', u'/app//keypress', u'/app//keypress', u'/app//keypress', u'/app//keypress', u'/app//keypress', u'/app//keypress', u'/live/virtualzappinglistbanner/zap', u'/live/virtualzappinglistbanner/view', u'/live/digit/zap', u'/live/noright/view', u'/live/digit/zap', u'/live/ctazap/view', u'/live/ctazap/click', u'/live/toolbox/view', u'/app//keypress', u'/live/toolbox/click', u'/isf//activate', u'/app//keypress', u'/live//activate', u'/live//zap', u'/live/ctazap/view', u'/live/ctazap/click', u'/live/toolbox/view', u'/app//keypress', u'/app//keypress', u'/app//keypress', u'/app//standby', u'/app/favorites/state', u'/app/optin/state', u'/app/perso/state', u'/app/tnt/state', u'/app/hdd/state', u'/app/switches/state', u'/qos/live/', u'/trap//temperature']
expectedEvents=[u'/app//wakeup', u'/app/hdmi/state', u'/homepag/ws/view', u'/homepage//activate', u'/live//activate', u'/live//zap', u'/live/ppluspminus/zap', u'/app//keypress', u'/live/virtualzappinglistbanner/zap', u'/live/virtualzappinglistbanner/view', u'/live/digit/zap', u'/live/noright/view', u'/live/digit/zap', u'/live/toolbox/view', u'/live/toolbox/click', u'/isf//activate', u'/app//keypress', u'/live//activate', u'/live//zap', u'/live/toolbox/view', u'/live/toolbox/click', u'/app//standby', u'/app/favorites/state', u'/app/optin/state', u'/app/perso/state', u'/app/tnt/state', u'/app/hdd/state', u'/app/switches/state', u'/qos/live/', u'/trap//temperature']
indexR = 0
indexE = 0
n_expected_event=['ctazap']
while indexR!=len(recordedEvents)-1:
if recordedEvents[indexR]==expectedEvents[indexE]:
print "[event]"+str(expectedEvents[indexE])+" Matching OK"
indexE+=1
indexR+=1
else:
for diff in n_expected_event:
if str(diff) in str(recordedEvents[indexR]):
print"[missing] "+str(recordedEvents[indexR])
indexR+=1
结果是:
[event]/app//wakeup Matching OK
[event]/app/hdmi/state Matching OK
[event]/homepage/ws/view Matching OK
[event]/homepage//activate Matching OK
[event]/live//activate Matching OK
[event]/live//zap Matching OK
[missing] /live/ctazap/view
[event]/live/ppluspminus/zap Matching OK
[event]/app//keypress Matching OK
[event]/live/virtualzappinglistbanner/zap Matching OK
[event]/live/virtualzappinglistbanner/view Matching OK
[event]/live/digit/zap Matching OK
[event]/live/noright/view Matching OK
[event]/live/digit/zap Matching OK
[missing] /live/ctazap/view
[missing] /live/ctazap/click
[event]/live/toolbox/view Matching OK
[event]/live/toolbox/click Matching OK
[event]/isf//activate Matching OK
[event]/app//keypress Matching OK
[event]/live//activate Matching OK
[event]/live//zap Matching OK
[missing] /live/ctazap/view
[missing] /live/ctazap/click
[event]/live/toolbox/view Matching OK
提前谢谢
答案 0 :(得分:1)
您可以使用set
操作来执行此操作。您要求的内容基本上是set.intersection
和set.difference
。
>>> set(recordedEvents).intersection(expectedEvents)
{'/app/optin/state',
'/live/virtualzappinglistbanner/zap',
'/qos/live/',
'/live/ppluspminus/zap',
'/live/noright/view',
'/live/digit/zap',
'/live//zap',
'/homepage//activate',
'/trap//temperature',
'/app/tnt/state',
'/app//standby',
'/app/switches/state',
'/live/toolbox/view',
'/app/perso/state',
'/live/virtualzappinglistbanner/view',
'/app//wakeup',
'/app/hdd/state',
'/app/favorites/state',
'/app/hdmi/state',
'/isf//activate',
'/app//keypress',
'/live//activate',
'/live/toolbox/click'}
>>> set(recordedEvents).difference(expectedEvents)
{'/live/ctazap/view',
'/live/ctazap/click',
'/homepage/ws/view'}
然后你不需要自己做任何迭代。如果您仍想打印出格式,可以这样做。
>>> for i in set(recordedEvents).intersection(expectedEvents):
print('[event]{} Matching OK'.format(i))
[event]/app/optin/state Matching OK
[event]/live/virtualzappinglistbanner/zap Matching OK
[event]/qos/live/ Matching OK
[event]/live/ppluspminus/zap Matching OK
[event]/live/noright/view Matching OK
[event]/live/digit/zap Matching OK
[event]/live//zap Matching OK
[event]/homepage//activate Matching OK
[event]/trap//temperature Matching OK
[event]/app/tnt/state Matching OK
[event]/app//standby Matching OK
[event]/app/switches/state Matching OK
[event]/live/toolbox/view Matching OK
[event]/app/perso/state Matching OK
[event]/live/virtualzappinglistbanner/view Matching OK
[event]/app//wakeup Matching OK
[event]/app/hdd/state Matching OK
[event]/app/favorites/state Matching OK
[event]/app/hdmi/state Matching OK
[event]/isf//activate Matching OK
[event]/app//keypress Matching OK
[event]/live//activate Matching OK
[event]/live/toolbox/click Matching OK
>>> for i in set(recordedEvents).difference(expectedEvents):
print('[missing] {}'.format(i))
[missing] /live/ctazap/view
[missing] /live/ctazap/click
[missing] /homepage/ws/view