获取两个列表之间的区别?

时间:2014-02-06 10:10:38

标签: python

我必须列出数字,我想保存每个并行单元格之间的差异 我需要这样的东西:

diffEx = [(myEx - opEx) for myEx,opEx in (myExeptPack,opExeptPack)]

例如。

listA = [1,2,3]
listB = [4,3,2]
diff = [-3,-1,1]

谢谢

2 个答案:

答案 0 :(得分:6)

使用zip功能:

diffEx = [(myEx - opEx) for myEx,opEx in zip(myExeptPack,opExeptPack)]

答案 1 :(得分:3)

您可以使用map使语句比使用zip时更简洁:

import operator
diffEx = map(operator.sub, myExeptPack, opExeptPack)