在Terraform中,我需要从以下位置转换我的输入数据结构:
vip_sets = [
["1.0.1.1", "1.0.2.1", "1.0.0.1"]
["1.0.1.2", "1.0.2.2", "1.0.0.2"]
["1.0.1.3", "1.0.2.3", "1.0.0.3"]
["1.0.1.4", "1.0.2.4", "1.0.0.4"]
]
产生这样的输出:
df.style.applymap(alta_baja, subset=['MEDIA']).format({'MEDIA': "{:.1f}"})
所以从本质上讲,我需要获取列表的输入列表并创建一个输出,该输出再次是列表的列表,但其第0个列表是输入中每个列表中第0个元素的列表...然后第一次同样如此,依此类推。 我无法预先知道输入中将包含多少个列表,或者它们将有多长,但是我们可以假设这些列表的长度都是一样的。
我已经尝试了几乎所有我能想到的东西,并在网上搜索了,但是从此没有运气。所有建议都将受到欢迎!
答案 0 :(得分:1)
这有点可怕,但是可以解决(尽管我没有测试vip_lists
为空的情况。可能崩溃,因为我在不检查的情况下索引到vip_lists[0]
) :
locals {
vip_lists = [
["1.0.1.1", "1.0.1.2", "1.0.1.3", "1.0.1.4"],
["1.0.2.1", "1.0.2.2", "1.0.2.3", "1.0.2.4"],
["1.0.0.1", "1.0.0.2", "1.0.0.3", "1.0.0.4"]
]
vip_sets = [for i in range(0, length(local.vip_lists[0])): [for j in range(0, length(local.vip_lists)): local.vip_lists[j][i]]]
}
output "vip_sets" {
value = local.vip_sets
}
$ terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
vip_sets = [
[
"1.0.1.1",
"1.0.2.1",
"1.0.0.1",
],
[
"1.0.1.2",
"1.0.2.2",
"1.0.0.2",
],
[
"1.0.1.3",
"1.0.2.3",
"1.0.0.3",
],
[
"1.0.1.4",
"1.0.2.4",
"1.0.0.4",
],
]
答案 1 :(得分:1)
我曾经为github.com/mineiros-io上的一个模块长度不相同的列表列表编写了该版本的列表,在该列表中,我们使用count
使用此类转换来创建二维资源集。 (那些未使用的atm,因为我们将它们转换为可用于资源级别for_each
的地图)。
locals {
matrix = [
["1.0.1.1", "1.0.1.4"],
["1.0.2.1", "1.0.2.2", "1.0.2.3", "1.0.2.4"],
["1.0.0.1", "1.0.0.3", "1.0.0.4"]
]
row_lengths = [
for row in local.matrix : length(row)
]
max_row_length = max(0, local.row_lengths...)
output = [
for i in range(0, local.max_row_length) : [
for j, _ in local.matrix : try(local.matrix[j][i], null)
]
]
output_compact = [
for i in range(0, local.max_row_length) : compact([
for j, _ in local.matrix : try(local.matrix[j][i], null)
])
]
}
output "matrix" {
value = local.output
}
output "compact" {
value = local.output_compact
}
可以处理动态列表大小,并以紧凑的形式或以null
值填充来输出:
Outputs:
compact = [
[ "1.0.1.1", "1.0.2.1", "1.0.0.1" ],
[ "1.0.1.4", "1.0.2.2", "1.0.0.3" ],
[ "1.0.2.3", "1.0.0.4" ],
[ "1.0.2.4" ],
]
matrix = [
[ "1.0.1.1", "1.0.2.1", "1.0.0.1" ],
[ "1.0.1.4", "1.0.2.2", "1.0.0.3" ],
[ null, "1.0.2.3", "1.0.0.4" ],
[ null, "1.0.2.4", null ],
]
我知道答案已经被接受,但是也许有人仍然可以使用此动态版本。