如何从数学上减去python中的两个列表? [英] How to mathematically subtract two lists in python?
本文介绍了如何从数学上减去python中的两个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道python不支持列表减法,但是有一些方法可以省略两个列表之间的公共元素.但是我要做的是将一个列表中的每个元素分别减去另一列表中的对应元素,然后将结果作为输出列表返回.我该怎么办?
A = [3, 4, 6, 7]
B = [1, 3, 6, 3]
print A - B #Should print [2, 1, 0, 4]
解决方案
>>> A = [3, 4, 6, 7]
>>> B = [1, 3, 6, 3]
>>> map(operator.sub, A, B)
[2, 1, 0, 4]
正如下面提到的 @SethMMorton 一样,在Python 3中,您需要使用它
>>> A = [3, 4, 6, 7]
>>> B = [1, 3, 6, 3]
>>> list(map(operator.sub, A, B))
[2, 1, 0, 4]
因为,Python中的 map 会返回迭代器.>
I know subtraction of lists is not supported in python, however there are some ways to omit the common elements between two lists. But what I want to do is subtraction of each element in one list individually with the corresponding element in another list and return the result as an output list. How can I do this?
A = [3, 4, 6, 7]
B = [1, 3, 6, 3]
print A - B #Should print [2, 1, 0, 4]
解决方案
>>> A = [3, 4, 6, 7]
>>> B = [1, 3, 6, 3]
>>> map(operator.sub, A, B)
[2, 1, 0, 4]
As @SethMMorton mentioned below, in Python 3, you need this instead
>>> A = [3, 4, 6, 7]
>>> B = [1, 3, 6, 3]
>>> list(map(operator.sub, A, B))
[2, 1, 0, 4]
Because, map in Python returns an iterator instead.
这篇关于如何从数学上减去python中的两个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文