【发布时间】:2012-02-21 16:27:49
【问题描述】:
我有两个测试结果列表。测试结果表示为字典:
list1 = [{testclass='classname', testname='testname', testtime='...},...]
list2 = [{testclass='classname', testname='testname', ...},...]
两个列表中的字典表示略有不同,因为对于一个列表,我有一些 更多信息。但在所有情况下,任一列表中的每个测试字典都会有一个 classname 和 testname 元素,它们共同有效地形成了一种唯一标识测试的方法以及一种在列表之间进行比较的方法。
我需要找出所有在 list1 中但不在 list2 中的测试,因为它们代表新的测试失败。
我这样做:
def get_new_failures(list1, list2):
new_failures = []
for test1 in list1:
for test2 in list2:
if test1['classname'] == test2['classname'] and \
test1['testname'] == test2['testname']:
break; # Not new breakout of inner loop
# Doesn't match anything must be new
new_failures.append(test1);
return new_failures;
我想知道这是一种更 Python 的方式。我看了过滤器。过滤器使用的函数需要获得两个列表的句柄。一个很容易,但我不确定如何处理这两个问题。我确实知道列表的内容,直到运行时。
任何帮助将不胜感激,
谢谢。
【问题讨论】:
标签: python list dictionary