SML:从一个列表中减去另一个列表
从一个列表中减去另一个列表的最简单方法是什么?我需要使用 ListPair 来解决这个任务吗?请注意,我需要比较行,而不是单个元素。例如,有两个列表“L1”和“L2”:
L1 =
[(1, 2, 3),
(4, 5, 6)]
L2 =
[(1, 2, 3),
(4, 5, 6),
(3, 2, 3]
我需要通过应用L3 = L2-L1
来获得“L3”:
L3 =
[(3, 2, 3)]
谢谢。
What is the easiest way to subtract one list from another? Do I need to use ListPair to solve this task? Notic that I need to compare ROWS, not single elements. For instance, there are two lists "L1" and "L2":
L1 =
[(1, 2, 3),
(4, 5, 6)]
L2 =
[(1, 2, 3),
(4, 5, 6),
(3, 2, 3]
I need to get "L3" by applying L3 = L2-L1
:
L3 =
[(3, 2, 3)]
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解这个问题,您想要删除 L2 中也在 L1 中的元素,但每次只删除一次。
一个简单的解决方案可能涉及一个辅助函数来告诉您是否在 L1 中找到了某个元素,以及删除了该元素的 L1 的其余部分。
现在您可以迭代 L2,每次 remFirst 返回 true 时丢弃元素,然后继续处理列表的其余部分。
相反,如果您想删除 L2 与 L1 共有的前缀,事情会变得更简单一些。
更新:问题现在已更改。
如果现在要求从 L2 中删除 L1 中的元素,则过滤器很有用。
As I understand the question, you want to remove the elements in L2 which are also in L1, but only once per occurrence.
A simple solution might involve a helper function to tell you if an element was found in L1, along with the rest of L1 with this element removed.
Now you can iterate through L2, discarding elements each time remFirst returns true, and then proceeding with the rest of the list.
If instead you want to remove the prefix which L2 has in common with L1, things get a bit simpler.
UPDATE: The question has now been altered.
If the requirement is now to remove elements from L2 that are in L1, filter is useful.