如何在python中减去两个列表 [英] How to subtract two lists in python

查看:213
本文介绍了如何在python中减去两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

,结果应该是一个从List1 List3=[0,-2,4]中减去List2的新列表! 我知道,我必须以某种方式使用zip功能.通过这样做,我得到: ([(3,3), (5,7), (6,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?

推荐答案

尝试一下:

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

这使用了zip,列出了内容并进行了结构分解.

This uses zip, list comprehensions, and destructuring.

这篇关于如何在python中减去两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆