company_list = ast.literal_eval(line[18])
print company_list
for i in company_list:
#print type(i)
print i["isin"]
我必须从这个groovy脚本将string []传递给另一个java函数。所以我试图读取数组列表,然后将其转换为String数组。但问题是我将def map = new HashMap<String,List<String>>()
def list = new ArrayList<String>()
def list1 = new ArrayList<String>()
list.add("hello1")
list.add("world1")
list.add("sample1")
list.add("sample1")
list1.add("hello2")
list1.add("world2")
list1.add("sample2")
list1.add("sample2")
map.put("abc",list)
map.put("bcd",list1)
def data = new ArrayList<String>()
for(e in map){
println "key = ${e.key} value=${e.value}"
// data = "${e.value} as String[]"
data = "${e.value}"
println "size ${data.size()} " --(B)
check(data)
}
def check(input)
{
println "${input.size()}" ---(A)
for(item in input){
print "$item "}
}
分配给变量{e.value}
并尝试获取大小data
(步骤(A)和(B))。和data.size()
。它从列表中计算每个字符而不是整个单词。我想迭代列表中的每个单词。请让我知道如何解决这个问题。
样本输出
size is 34
答案 0 :(得分:1)
这是创建地图并访问它的groovified vesion:
def map = [abc:['hello1', 'world1','sample1', 'sample1'], bcd:['hello2', 'world2','sample2', 'sample2']]
map.collect{ println "key: ${it.key}, list: ${it.value}, and size: ${it.value.size()}" }
<强>输出:强>
key: abc, list: [hello1, world1, sample1, sample1], and size: 4
key: bcd, list: [hello2, world2, sample2, sample2], and size: 4
如果您想将列表转换为数组,您可以这样做:
def list = ['hello1', 'world1','sample1', 'sample1']
assert list instanceof List
def array = list as String[]
assert array instanceof String[]
assert !(array instanceof List)