groovy 中的转置列表
Transpose list in groovy
我想在 groovy 脚本中转置此列表。
有两个列表
result_name = ["API(示例)","管理门户(示例)","Component1","Component2",]
result_ids = ["3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]
我期待像 list[0][0]、list[1][1] 这样的输出....
示例:
API (example) 3wrhs4vp3sp5
Management Portal g2828br1gzw9
Component1 68pnwhltxcq0
Component2 fy8g2nvvdg15
我正在尝试使用
def result = [[result_name], [result_ids]].transpose()
但结果是:
Result: [[["API (example)","Management Portal (example)","Component1","Component2",], ["3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]]]
编辑:更新了问题中的示例代码:
proc1 = ['/bin/bash', '-c', "curl https://api.statuspage.io/v1/pages/lbh0g6b5mwnf/components?api_key=<key>"].execute()
proc2 = ['/bin/bash', '-c', "grep -Po '\"name\": *\K\"[^\"]*\"'| tr '\n' ', '"].execute()
proc3 = ['/bin/bash', '-c', "curl https://api.statuspage.io/v1/pages/lbh0g6b5mwnf/components?api_key=<key>"].execute()
proc4 = ['/bin/bash', '-c', "grep -Po '\"id\": *\K\"[^\"]*\"'| tr '\n' ', '"].execute()
all_name = proc1 | proc2
all_ids = proc3 | proc4
def result_name = [all_name.text]
def result_ids = [all_ids.text]
println result_name
println result_ids
def result = [result_name, result_ids].transpose()
结果
["API (example)","Management Portal (example)","Component1","Component2",]
["3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]
Result: [["API (example)","Management Portal (example)","Component1","Component2",, "3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]]
在您显示的代码示例中,您将 result_name
和 result_ids
列表包装到附加列表中。重写以下代码:
def result = [[result_name], [result_ids]].transpose()
至:
def result = [result_name, result_ids].transpose()
你会得到预期的结果。
回答我自己的问题。
经过几次调试后,我意识到我的列表只有一个字符串,因此 split() 没有预料到奇怪的输入。我能够通过输入 newstr
然后 split(',') 下面的代码来打印第一个字符串:
all_name = proc1 | proc2
all_ids = proc3 | proc4
result_name = [all_name.text]
result_ids = [all_ids.text]
newstr = result_name[0]
result_newstr = newstr.split(',')
newids = result_ids[0]
result_newids = newids.split(',')
def aa = []
for(int i = 0;i< result_newstr.size(); i++) {
a = result_newstr[i].concat(" ").concat(result_newids[i])
aa.add(a)
}
return aa
结果:
"API (example)" "3wrhs4vp3sp5"
"Management Portal (example)" "g2828br1gzw9"
"Component1" "68pnwhltxcq0"
"Component2" "fy8g2nvvdg15"
我想在 groovy 脚本中转置此列表。 有两个列表
result_name = ["API(示例)","管理门户(示例)","Component1","Component2",]
result_ids = ["3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]
我期待像 list[0][0]、list[1][1] 这样的输出.... 示例:
API (example) 3wrhs4vp3sp5
Management Portal g2828br1gzw9
Component1 68pnwhltxcq0
Component2 fy8g2nvvdg15
我正在尝试使用
def result = [[result_name], [result_ids]].transpose()
但结果是:
Result: [[["API (example)","Management Portal (example)","Component1","Component2",], ["3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]]]
编辑:更新了问题中的示例代码:
proc1 = ['/bin/bash', '-c', "curl https://api.statuspage.io/v1/pages/lbh0g6b5mwnf/components?api_key=<key>"].execute()
proc2 = ['/bin/bash', '-c', "grep -Po '\"name\": *\K\"[^\"]*\"'| tr '\n' ', '"].execute()
proc3 = ['/bin/bash', '-c', "curl https://api.statuspage.io/v1/pages/lbh0g6b5mwnf/components?api_key=<key>"].execute()
proc4 = ['/bin/bash', '-c', "grep -Po '\"id\": *\K\"[^\"]*\"'| tr '\n' ', '"].execute()
all_name = proc1 | proc2
all_ids = proc3 | proc4
def result_name = [all_name.text]
def result_ids = [all_ids.text]
println result_name
println result_ids
def result = [result_name, result_ids].transpose()
结果
["API (example)","Management Portal (example)","Component1","Component2",]
["3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]
Result: [["API (example)","Management Portal (example)","Component1","Component2",, "3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]]
在您显示的代码示例中,您将 result_name
和 result_ids
列表包装到附加列表中。重写以下代码:
def result = [[result_name], [result_ids]].transpose()
至:
def result = [result_name, result_ids].transpose()
你会得到预期的结果。
回答我自己的问题。
经过几次调试后,我意识到我的列表只有一个字符串,因此 split() 没有预料到奇怪的输入。我能够通过输入 newstr
然后 split(',') 下面的代码来打印第一个字符串:
all_name = proc1 | proc2
all_ids = proc3 | proc4
result_name = [all_name.text]
result_ids = [all_ids.text]
newstr = result_name[0]
result_newstr = newstr.split(',')
newids = result_ids[0]
result_newids = newids.split(',')
def aa = []
for(int i = 0;i< result_newstr.size(); i++) {
a = result_newstr[i].concat(" ").concat(result_newids[i])
aa.add(a)
}
return aa
结果:
"API (example)" "3wrhs4vp3sp5"
"Management Portal (example)" "g2828br1gzw9"
"Component1" "68pnwhltxcq0"
"Component2" "fy8g2nvvdg15"