我有字典列表。我希望類型遍歷這個列表並將其映射到稍微不同類型的字典。將字典列表從一種類型映射到另一種字典
原詞典:
{'Test Name':blah, 'Test': blah, 'Att a': blah, 'Att b': blah}
新字典:
{'Test Name':blah, 'Test': blah, 'Failure': failure, 'Project': project}
在原來的我只在乎屬性測試名稱和測試。在新的詞典中,我添加了一些額外的屬性。
要做到這一點,我做的:
mappedFailures = [];
for fixedFailure in fixedFailures:
mappedFailure = {'Test Suite': fixedFailure['Test Suite'],
'Test':fixedFailure['Test'], 'Failure':failure,
'Project': 'project'}
mappedFailures.append(mappedFailure);
這是很容易閱讀,但我不知道有沒有一個更Python的方式感謝。
Python 3包含dict comprehensions,它是爲這種用例設計的。請參閱http://www.python.org/dev/peps/pep-0274/ – 2012-02-05 20:54:29
第一個「pythonic」要做的是放棄分號。 – 2012-02-05 23:12:57