如何根据Groovy中列的值过滤列表列表的元素

时间:2017-11-02 04:56:49

标签: list groovy

我正在使用Groovy并使list of list具有一定数量的元素:

def rList = [["Using Patient Feedback","Completed","No Results Available","Condition1","Other","Phase I","18 Years and older   (Adult, Senior)","Other","https://folder1//folder2"],["The Effect of a Education ","Completed","No Results Available","Condition1","Behavioral","Glycemic Control","18 Years and older   (Adult, Senior)","Other","https://folder1//folder3"],["The Public Private Partnership","Completed","No Results Available","Condition1","Enhanced Education","Improvement in A1C","18 Years to 85 Years   (Adult, Senior)","NIH","https://folder1//folder4"],["Evaluation of a Pilot","Completed","No Results Available","Condition1","Behavioral","Change in percent of total calories","10 Years to 19 Years   (Child, Adult)","Other","https://folder1//folder5"],["Self Management Skills","Completed","No Results Available","Condition1","Behavioral","Metabolic control","18 Years and older   (Adult, Senior)","Industry","https://folder1//folder6"]]

包含上述list的每个list of list都有9个元素/列。我还有一个搜索字符串定义如下:

def fundingType = "NIH"

现在我要做的是在每个列表的8th元素/列中查找此字符串是否存在,如果存在,则将其余8个元素存储在8个不同的变量中var1var2,...,var8。我怎样才能在Groovy

中执行此操作

2 个答案:

答案 0 :(得分:2)

您应该可以使用find轻松获取它,如下所示:

def fundingType = "NIH"
println rList.find{ fundingType == it[7] }

您可以在线快速尝试 demo

您可以将其更改为:

,而不是8个变量
def result = rList.find{ fundingType == it[7] }

并使用result[0]result[1] ...而不使用额外的8个变量。

答案 1 :(得分:2)

类似的东西:

def rList = [["Using Patient Feedback","Completed","No Results Available","Condition1","Other","Phase I","18 Years and older   (Adult, Senior)","Other","https://folder1//folder2"],["The Effect of a Education ","Completed","No Results Available","Condition1","Behavioral","Glycemic Control","18 Years and older   (Adult, Senior)","Other","https://folder1//folder3"],["The Public Private Partnership","Completed","No Results Available","Condition1","Enhanced Education","Improvement in A1C","18 Years to 85 Years   (Adult, Senior)","NIH","https://folder1//folder4"],["Evaluation of a Pilot","Completed","No Results Available","Condition1","Behavioral","Change in percent of total calories","10 Years to 19 Years   (Child, Adult)","Other","https://folder1//folder5"],["Self Management Skills","Completed","No Results Available","Condition1","Behavioral","Metabolic control","18 Years and older   (Adult, Senior)","Industry","https://folder1//folder6"]]
def fundingType = "NIH"
def fundingColumnIndex = 7

def fundingRow = rList.find { it[fundingColumnIndex] == fundingType }
fundingRow.remove(fundingColumnIndex)
assert fundingRow.size() == 8
def (v1, v2, v3, v4, v5, v6, v7, v8) = fundingRow
[v1, v2, v3, v4, v5, v6, v7, v8].each { println(it) }

输出:

The Public Private Partnership
Completed
No Results Available
Condition1
Enhanced Education
Improvement in A1C
18 Years to 85 Years   (Adult, Senior)
https://folder1//folder4