python中两个列表相减的方法

发布于 2024-12-17 03:33:24 字数 240 浏览 0 评论 0原文

我不知道如何在 python 中创建一个可以计算这个的函数:

List1=[3,5,6]
List2=[3,7,2]

结果应该是一个从 List1 中减去 List2 的新列表,List3=[0,-2,4]! 我知道,我必须以某种方式使用 zip 功能。通过这样做我得到: ([(3,3), (5,7), (6,2)]),但我不知道现在该怎么办?

I can't figure out how to make a function in python that can calculate this:

List1=[3,5,6]
List2=[3,7,2]

and the result should be a new list that substracts List2 from List1, List3=[0,-2,4]!
I know, that I somehow have to use the zip-function. By doing that I get:
([(3,3), (5,7), (6,2)]), but I don't know what to do now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心如狂蝶 2024-12-24 03:33:24

试试这个:

[x1 - x2 for (x1, x2) in zip(List1, List2)]

它使用 zip、列表推导式和解构。

Try this:

[x1 - x2 for (x1, x2) in zip(List1, List2)]

This uses zip, list comprehensions, and destructuring.

も星光 2024-12-24 03:33:24

此解决方案使用 numpy。它仅对于较大的列表才有意义,因为实例化 numpy 数组会产生一些开销。 OTOH,对于除了简短列表之外的任何内容,这都将非常快。

>>> import numpy as np
>>> a = [3,5,6]
>>> b = [3,7,2]
>>> list(np.array(a) - np.array(b))
[0, -2, 4]

This solution uses numpy. It makes sense only for largish lists as there is some overhead in instantiate the numpy arrays. OTOH, for anything but short lists, this will be blazingly fast.

>>> import numpy as np
>>> a = [3,5,6]
>>> b = [3,7,2]
>>> list(np.array(a) - np.array(b))
[0, -2, 4]
恰似旧人归 2024-12-24 03:33:24

您可以按照@Matt的建议使用列表理解。您还可以使用 itertools - 更具体地说,是 imap() 函数:

>>> from itertools import imap
>>> from operator import sub
>>> a = [3,5,6]
>>> b = [3,7,2]
>>> imap(int.__sub__, a, b)
<itertools.imap object at 0x50e1b0>
>>> for i in imap(int.__sub__, a, b):
...     print i
... 
0
-2
4

与所有 itertools 函数一样,imap() 返回一个迭代器。您可以生成一个列表,将其作为 list() 构造函数的参数传递:

>>> list(imap(int.__sub__, a, b))
[0, -2, 4]
>>> list(imap(lambda m, n: m-n, a, b)) # Using lambda
[0, -2, 4]

编辑:正如下面 @Cat 所建议的,最好使用 带有imap()的operator.sub()函数:

>>> from operator import sub
>>> list(imap(sub, a, b))
[0, -2, 4]

You can use list comprehension, as @Matt suggested. you can also use itertools - more specifically, the imap() function:

>>> from itertools import imap
>>> from operator import sub
>>> a = [3,5,6]
>>> b = [3,7,2]
>>> imap(int.__sub__, a, b)
<itertools.imap object at 0x50e1b0>
>>> for i in imap(int.__sub__, a, b):
...     print i
... 
0
-2
4

Like all itertools funcitons, imap() returns an iterator. You can generate a list passing it as a parameter for the list() constructor:

>>> list(imap(int.__sub__, a, b))
[0, -2, 4]
>>> list(imap(lambda m, n: m-n, a, b)) # Using lambda
[0, -2, 4]

EDIT: As suggested by @Cat below, it would be better to use the operator.sub() function with imap():

>>> from operator import sub
>>> list(imap(sub, a, b))
[0, -2, 4]
月光色 2024-12-24 03:33:24

下面还有另一个解决方案:

>>> a = [3,5,6]
>>> b = [3,7,2]
>>> list(map(int.__sub__, a, b)) # for python3.x
[0, -2, 4]
>>> map(int.__sub__, a, b) # and for python2.x
[0, -2, 4]

添加: 只需检查 python 参考map 你会发现你可以将多个迭代传递给 map

Yet another solution below:

>>> a = [3,5,6]
>>> b = [3,7,2]
>>> list(map(int.__sub__, a, b)) # for python3.x
[0, -2, 4]
>>> map(int.__sub__, a, b) # and for python2.x
[0, -2, 4]

ADDITION: Just check for the python reference of map and you'll see you could pass more than one iterable to map

南薇 2024-12-24 03:33:24

您可以按照以下方式

List1 = [3,5,6]
List2 = [3,7,2]
ans = [List1[i]-List2[i] for i in range(min(len(List1), len(List2)))]
print ans

输出 [0, -2, 4]

You can do it in the following way

List1 = [3,5,6]
List2 = [3,7,2]
ans = [List1[i]-List2[i] for i in range(min(len(List1), len(List2)))]
print ans

that outputs [0, -2, 4]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文