简体   繁体   English

列出对数组有条件的理解?

[英]List Comprehensions with a condition on an array?

I have an array that contains tuples that look like this: [((Int, Int), Int)] . 我有一个包含如下元组的数组: [((Int, Int), Int)] I am trying to create a new array with list comprehensions that says to only add all 3 Ints if the 3rd Int == a certain number. 我正在尝试创建一个具有列表理解的新数组,该数组表示如果第三个Int ==一定数量,则仅添加所有3个Int。 I have it written as 我写成

newArray = [((x,y),z) | ((x,y),z)<-oldArray, (snd oldArray) == 5]

However, when I try to run the code that contains this, it says "couldn't match expected type" and point the error at my conditional. 但是,当我尝试运行包含此代码的代码时,它说“无法匹配预期的类型”,并将错误指向我的条件代码。 "Couldn't match expected type '(a0, Int)' with actual type '[((Int, Int), Int)]'" . "Couldn't match expected type '(a0, Int)' with actual type '[((Int, Int), Int)]'"

oldArray is a list, always and forever, even inside the comprehension, so snd can't be applied to it. oldArray是一个列表,即使在理解范围内,也始终存在,因此无法将snd应用于它。 Use z == 5 instead. 使用z == 5代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM