我想在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_name], [result_ids]] .transpose ()
,但结果是:
结果:[[["API(示例)"管理门户(示例)" 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
打印第一个字符串,然后在下面分割(',')代码:
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"