简体   繁体   English

将值放入元组列表中

[英]Get values into list of tuples

I have the following structure which is produced by yamldecode(file("myfile.yaml")) and I use Terraform.我有以下由 yamldecode(file("myfile.yaml")) 生成的结构,我使用 Terraform。

[
  {
    "name" = "A"
    "number" = "1"
    "description" = "Bla"
  },
  {
    "name" = "B"
    "number" = "2"
    "description" = "Bla"
  },
]

Initial yaml looks like:初始 yaml 看起来像:

test:
  - name: "A"
    number: '1'
    description: "Bla"
  - name: "B"
    number: '2'
    description: "Bla"

I need to get values from all maps in a list of tuples.我需要从元组列表中的所有地图中获取值。 Please advice请指教

Expected result:预期结果:

("A", 1, "Bla"), ("B", 2, "Bla")

You can load YAML file as dict using pyyaml library and itterate over data.您可以使用 pyyaml 库将 YAML 文件加载为 dict 并遍历数据。
Install pyyaml using pip install pyyaml使用pip install pyyaml 安装 pyyaml

import yaml

with open("test.yaml", "r") as stream:
    data_list_dict = yaml.safe_load(stream)

output_list = []
for data_dict in data_list_dict['test']:
    output_list.append(tuple(data_dict.values()))

print(output_list)

Output:输出:

[('1', 'Bla', 'A'), ('2', 'Bla', 'B')]

Or或者

import yaml

with open("test.yaml", "r") as stream:
    data_list_dict = yaml.safe_load(stream)

output_list = [tuple(data_dict.values()) for data_dict in data_list_dict['test']]
print(output_list)

If order is important you can do the following:如果顺序很重要,您可以执行以下操作:

locals {
   
    test = [
  {
    "name" = "A"
    "number" = "1"
    "description" = "Bla"
  },
  {
    "name" = "B"
    "number" = "2"
    "description" = "Bla"
  },
]

  list_of_lists = [for a_map in local.test: 
            [a_map["name"], a_map["number"], a_map["description"]]]
    
}

which gives:这使:

[
  [
    "A",
    "1",
    "Bla",
  ],
  [
    "B",
    "2",
    "Bla",
  ],
]

From your question it's not clear whether you're asking how to solve this problem in the Terraform language or in Python -- you only mentioned Terraform in the question body, but you tagged it "python" and used Python syntax to show the tuples.从您的问题来看,不清楚您是在询问如何用 Terraform 语言或 Python 解决这个问题——您只在问题正文中提到了 Terraform,但您将其标记为“python”并使用 Python 语法来显示元组。

Others have already shown how to do this in Python, so just in case you were asking about Terraform the following is a Terraform equivalent:其他人已经展示了如何在 Python 中执行此操作,因此以防万一您询问 Terraform,以下是 Terraform 的等效项:

locals {
  input = [
    {
      "name" = "A"
      "number" = "1"
      "description" = "Bla"
    },
    {
      "name" = "B"
      "number" = "2"
      "description" = "Bla"
    },
  ]

  list_of_tuples = tolist([
    for e in local.input : [e.name, e.number, e.description]
  ])
}

A tuple in Terraform is a kind of sequence type where the length is fixed and each element can have its own specified type. Terraform 中的元组是一种序列类型,其长度是固定的,每个元素都可以有自己指定的类型。 It's the sequence equivalent of an object, whereas a list corresponds with a map.它是对象的序列等价物,而列表对应于地图。

When evaluating the list_of_tuples expression above, Terraform will first construct a tuple of tuples (because [] brackets construct tuples) and then convert the outer tuple to be a list with tolist .在评估上面的list_of_tuples表达式时,Terraform 将首先构造一个元组元组(因为[]括号构造元组),然后将外部元组转换为带有tolist的列表。 The final type of list_of_tuples would therefore be the following, if written in Terraform's type constraint syntax :因此,如果使用Terraform 的类型约束语法编写,则list_of_tuples的最终类型将如下所示:

list(tuple([string, number, string]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM