我正在使用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个不同的变量中var1
, var2
,...,var8
。我怎样才能在Groovy
?
答案 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