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

List 如何在Groovy中根据列的值过滤列表中的元素,list,groovy,List,Groovy,我使用的是Groovy,有一个列表,其中包含一定数量的元素: 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 ","Complete

我使用的是
Groovy
,有一个
列表
,其中包含一定数量的元素:

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"]]
每个
列表
以及上面的
列表
都有
9个
元素/列。我还有一个搜索字符串,定义如下:

def fundingType = "NIH"

现在我要做的是在每个列表的
8th
元素/列中查找该字符串的存在,如果它存在,则将其余8个元素存储在8个不同的变量中-
var1
var2
,…
var8
。如何在
Groovy
中执行此操作?

您应该能够使用
find
轻松获得它,如下所示:

def fundingType = "NIH"
println rList.find{ fundingType == it[7] }
你可以在网上快速试用​

代替8个变量,您可以将其更改为:

def result = rList.find{ fundingType == it[7] }

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

您应该能够使用
find
轻松获得它,如下所示:

def fundingType = "NIH"
println rList.find{ fundingType == it[7] }
你可以在网上快速试用​

代替8个变量,您可以将其更改为:

def result = rList.find{ fundingType == it[7] }
并使用
结果[0]
结果[1]
…而不使用额外的8个变量。

类似于:

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 公私伙伴关系 完整的 没有结果 条件1 强化教育 A1C的改进 18岁至85岁(成人、老年人) https://folder1//folder4 比如:

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 公私伙伴关系 完整的 没有结果 条件1 强化教育 A1C的改进 18岁至85岁(成人、老年人) https://folder1//folder4
你能检查解决方案看它是否有用吗?你能检查解决方案看它是否有用吗?为什么要先检查
中的
fundingType==it[7]
不够好,效率更高吗?@cfrick,谢谢你的意见。起初我只在
中使用了
,后来意识到OP指定了位置,所以最后添加了
条件。更新。为什么要先检查
中的
fundingType==it[7]
不够好,效率更高吗?@cfrick,谢谢你的意见。起初我只在
中使用了
,后来意识到OP指定了位置,所以最后添加了
条件。更新。