从另一个列表中减去一个列表 [英] Subtraction of one list of lists from another list of lists

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

问题描述

我想从另一个列表列表的对应元素中减去列表列表,例如:

I want to subtract a list of lists from corresponding elements of another list of lists such as:

a = [[1, 2], [3, 4]]
b = [[1, 2], [3, 0]]

预期输出为:

c = [[0, 0], [0, 4]]

仅需从另一个列表中减去一个列表就很容易了:

Subtraction with only one list from another is easy with:

c = [i - j for i, j in zip(a, b)]

但这不适用于列表列表,并返回 TypeError:-:"list"和"list" 不受支持的操作数类型.有关如何执行操作的任何想法?

but this isn't working for a list of lists and returns TypeError: unsupported operand type(s) for -: 'list' and 'list'. Any ideas on how to do it?

推荐答案

这应该可以完成:

c = [list(map(lambda x, y: x - y, ii, jj)) for ii, jj in zip(a, b)]

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

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