我的两个scala列表如下。这两个列表可能有不同的长度。
val a = List((1437377239637L,"192.168.1.4",5000), (1437377239637L,"192.168.1.4",35357), (1437377239637L,"192.168.1.4",43437), (1437377239637L,"192.168.1.4",53), (1437377239637L,"192.168.1.4",3306), (1437377239637L,"192.168.1.4",22))
和
val b = List((1437376639347L,"192.168.1.4",5000), (1437376639347L,"192.168.1.4",35357), (1437376639347L,"192.168.1.4",43437), (1437376639347L,"192.168.1.4",53), (1437376639347L,"192.168.1.4",3306), (1437376639347L,"192.168.1.4",22))
现在我想像这样过滤:
for {
(a1,b1,c) <- a
(d,e,f) <- b
if (b1!=e && c!=f)
} yield (d,e,f)
但是当我执行上面的代码时,它将返回为:
List[(Long, String, Int)] = List((1437376639347,"192.168.1.8",45), (1437376639347,"192.168.1.3",34), (1437376639347,"192.168.1.13",90), (1437376639347,"192.168.1.1",34), (1437376639347,"192.168.1.5",45), (1437376639347,"192.168.1.10",56), (1437376639347,"192.168.1.22",344),(1437376639347,"192.168.1.10",56), (1437376639347,"192.168.1.22",344), (143737...
结果混合了元组,并返回错误的结果。实际上结果应该是空列表,因为两个元组列表都包含相同的数据。如果我检查equals
if (b1==e && c==f)
,则会返回预期结果,但对于not equals
,它会混合数据。
有人知道这里的确切问题吗?我缺少什么?
编辑
另一个问题是每次两个列表不相同时都会检查这些新列表的数据量较少。
val a = List((1437377239637L,"192.168.1.4",5000), (1437377239637L,"192.168.1.4",35357), (1437377239637L,"192.168.1.4",43437), (1437377239637L,"192.168.1.4",53))
和
val b = List((1437376639347L,"192.168.1.4",5000), (1437376639347L,"192.168.1.4",35357), (1437376639347L,"192.168.100.4",43437))
和预期输出为
val result = List((1437377239637L,"192.168.1.4",43437), (1437377239637L,"192.168.1.4",53),(1437376639347L,"192.168.100.4",43437))
基本上我想找出two list of tuples set difference
答案 0 :(得分:2)
尝试使用更简单,更小的数据集进行测试,以便更容易了解正在进行的操作。
val a = List(1,2,3)
val b = a
for {
a1 <- a
b1 <- b
} yield (b1)
// Result: List(1, 2, 3, 1, 2, 3, 1, 2, 3)
您正在映射两个列表的产品。这听起来像你想要的是列表的拉链。
for {
(a1, b1) <- a zip b
} yield (a1, b1)
// Result: List((1,1), (2,2), (3,3))
添加过滤器,我们已完成:
for {
(a1, b1) <- a zip b
if (a1 != b1)
} yield b1
// Result: List()
答案 1 :(得分:0)
您需要先填写a
和b
列表,然后loop
填写条件:
for {
((a1, b1, c), (d, e, f)) <- a zip b
if (b1!=e && c!=f)
} yield (d,e,f)