以下是我的数据的虚构示例。当列表library(ggplot2)
library(data.table)
# Prepare data
# Create continuous data for the x-axis
dt[, X := 1:.N]
# Define begging of the year
break_labels <- dt[, min(X), year]
# Plot
ggplot(dt, aes(X, d)) +
# Use 0.5 to extend colour around the X
geom_rect(aes(xmin = X - 0.5, xmax = X + 0.5, ymin = -Inf, ymax = Inf, fill = time)) +
geom_point(size = 3) +
geom_line(size = 1, linetype = 2) +
# Specify wanted colour code
scale_fill_manual(values = c("#EDBB99", "#D6EAF8")) +
# Specify breaks only for the begging of the year
scale_x_continuous(breaks = break_labels$V1, labels = break_labels$year) +
labs(
x = "Year",
y = "Diversity",
fill = "Season"
) +
theme_classic()
中的任何字典具有名称为mylst.env
的键时,我想提取所有客户的名称
我想这可以通过嵌套循环来完成,但无法弄清楚如何实现。
n1
答案 0 :(得分:1)
json_query
过滤器是完成此类任务的好工具。把头缠起来需要一些时间,但可以尝试一下:
- set_fact:
cust_lst: "{{ mylst | json_query(query) }}"
vars:
query: "[?env[?name=='n1']].name"
您可以在official documentation上找到更多信息,并且可以无耻地插入blog post
答案 1 :(得分:0)
您可以在map
列表上使用item.env
过滤器来获取mylst
中每个元素包含的名称的列表。然后如果该列表中包含关键字“ n1”,则将其添加到final_var
中。请看下面
---
- hosts: localhost
gather_facts: false
vars:
mylst:
- apiVersion: v1
name: customer1
metaData:
cycles: 10
ships: 12
env:
- name: n1
value: v1
- name: n2
value: v2
- apiVersion: v1
name: customer2
metaData:
cycles: 10
ships: 12
env:
- name: n1
value: v1
- name: n3
value: v3
- apiVersion: v1
name: customer3
metaData:
cycles: 10
ships: 12
env:
- name: n3
value: v1
- name: n4
value: v4
- apiVersion: v1
name: customer4
metaData:
cycles: 10
ships: 12
env:
- name2: n3
value: v1
- name2: n4
value: v4
tasks:
- name: prepare var
set_fact:
final_var: "{{ final_var | default([]) + [item.name] }}"
when: '"n1" in {{ item.env | selectattr("name", "defined") | map(attribute="name") | list }}'
with_items:
- "{{ mylst }}"
- name: print var
debug:
var: final_var
您会注意到,我在mylst
中添加了第四个元素,该元素不包含键=“ name”的属性。在传递给地图过滤器之前,它们将由selectattr("name", "defined"
过滤器过滤掉。
PLAY [localhost] *******************************************************************************************************************************************************************************************************
TASK [prepare var] *****************************************************************************************************************************************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: "n1" in {{ item.env | selectattr("name", "defined") | map(attribute="name") | list }}
ok: [localhost] => (item={'apiVersion': 'v1', 'name': 'customer1', 'metaData': {'cycles': 10, 'ships': 12}, 'env': [{'name': 'n1', 'value': 'v1'}, {'name': 'n2', 'value': 'v2'}]})
ok: [localhost] => (item={'apiVersion': 'v1', 'name': 'customer2', 'metaData': {'cycles': 10, 'ships': 12}, 'env': [{'name': 'n1', 'value': 'v1'}, {'name': 'n3', 'value': 'v3'}]})
skipping: [localhost] => (item={'apiVersion': 'v1', 'name': 'customer3', 'metaData': {'cycles': 10, 'ships': 12}, 'env': [{'name': 'n3', 'value': 'v1'}, {'name': 'n4', 'value': 'v4'}]})
skipping: [localhost] => (item={'apiVersion': 'v1', 'name': 'customer4', 'metaData': {'cycles': 10, 'ships': 12}, 'env': [{'name2': 'n3', 'value': 'v1'}, {'name2': 'n4', 'value': 'v4'}]})
TASK [print var] *******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"final_var": [
"customer1",
"customer2"
]
}
PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
希望这些帮助。
发现此任务正在规避2.7.5错误:
- name: prepare var
set_fact:
final_var: "{{ final_var | default([]) + [item.name] }}"
when: "'n1' in item.env | selectattr('name', 'defined') | map(attribute='name') | list"
with_items:
- "{{ mylst }}"