从另一个列表中的对应值中减去一个列表中的值 [英] Subtract values in one list from corresponding values in another list
本文介绍了从另一个列表中的对应值中减去一个列表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个列表:
A = [2, 4, 6, 8, 10]
B = [1, 3, 5, 7, 9]
如何从另一个列表中的相应值中减去一个列表中的每个值,并创建一个列表,使得:
How do I subtract each value in one list from the corresponding value in the other list and create a list such that:
C = [1, 1, 1, 1, 1]
谢谢.
推荐答案
最简单的方法是使用列表理解
The easiest way is to use a list comprehension
C = [a - b for a, b in zip(A, B)]
或map()
:
from operator import sub
C = map(sub, A, B)
这篇关于从另一个列表中的对应值中减去一个列表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文