我正在构建 AWS EBS 卷属性列表,以便可以使用 jq 将其作为 CSV 存储在变量中。我要将变量输出到电子表格中。
第一个命令给出了我正在使用 jq 查找的值:
aws ec2 describe-volumes | jq -r '.Volumes[] | .VolumeId, .AvailabilityZone, .Attachments[].InstanceId, .Attachments[].State, (.Tags // [] | from_entries.Name)'
Run Code Online (Sandbox Code Playgroud)
给出我想要的输出,如下所示:
MIAPRBcdm0002_test_instance
vol-0105a1678373ae440
us-east-1c
i-0403bef9c0f6062e6
attached
MIAPRBcdwb00000_app1_vpc
vol-0d6048ec6b2b6f1a4
us-east-1c
MIAPRBcdwb00001 /carbon
vol-0cfcc6e164d91f42f
us-east-1c
i-0403bef9c0f6062e6
attached
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其转换为 CSV 格式以便可以将变量输出到电子表格,则该命令会崩溃并且不起作用:
aws ec2 describe-volumes | jq -r '.Volumes[] | .VolumeId, .AvailabilityZone, .Attachments[].InstanceId, .Attachments[].State, (.Tags // [] | from_entries.Name)| @csv'
jq: error (at <stdin>:4418): string ("vol-743d1234") cannot be csv-formatted, only array
Run Code Online (Sandbox Code Playgroud)
对于 EBS 卷,即使将 JSON 的顶层转换为 …
我想将对象父键放入对象本身并将每个键值对转换为数组
鉴于:
{
"field1": {
"key1": 11,
"key2": 10
},
"field2": {
"key1": 11,
"key2": 10
}
}
Run Code Online (Sandbox Code Playgroud)
所需输出
[
{"name": "field1", "key1": 11, "key2": 10},
{"name": "field2", "key1": 11, "key2": 10}
]
Run Code Online (Sandbox Code Playgroud)
我知道这jq keys
会给我["field1", "field2"]
并且jq '[.[]]'
会给我
[
{ "key1": 11, "key2": 10 },
{ "key1": 11, "key2": 10 }
]
Run Code Online (Sandbox Code Playgroud)
我无法找到将它们结合起来的方法,我该怎么做?
我在 sql 查询中使用了下表。
我想把它变成下面的形状
我该怎么做呢?我使用了 unnest 和 cross join 。由于数组为空,我没有看到整个视图。
我尝试解析以下变量:
变量.tf
variable "rbac_roles" {
type = object(
{
view = list(object({
group_name = string,
group_id = string,
namespaces = list(string)
})),
edit = list(object({
group_name = string,
group_id = string,
namespaces = list(string)
})),
admin = list(object({
group_name = string,
group_id = string,
namespaces = list(string)
}))
}
)
}
Run Code Online (Sandbox Code Playgroud)
变量.tfvars
rbac_roles = {
view = [
{
group_name = "group1",
group_id = "123",
namespaces = ["default", "namespace1"]
},
{
group_name = "group2",
group_id = "456",
namespaces = ["namespace2"] …
Run Code Online (Sandbox Code Playgroud) 我有一个元组和单个浮点数的列表
\nv = [(1., 2., 3.), (4., 5.), 6., 7.]\n
Run Code Online (Sandbox Code Playgroud)\n现在我想用旧的列表理解来压平它
\n[item for sublist in v for item in sublist]\n
Run Code Online (Sandbox Code Playgroud)\n但是,由于单个浮点不可迭代,因此会发生错误。展开这个混合列表的最有效方法是什么?我尝试遍历列表并使用,isininstance()
但再一次,该列表不可迭代。
编辑:到目前为止的时间比较
\n// 扁平化 JSON 对象 //(想要将嵌套哈希中的所有键合并为字符串,并希望将其输出到数组列表中)
input1 = {a: {b: {c: {} }, d:[] }, e: "e", f: nil, g: -2}
input2 = {a: {b: {c: {h: {j: ''}, m: {n: ''}}}, d: {k: {l: '' } }},e: "e",f: nil,g: -2}
Run Code Online (Sandbox Code Playgroud)
// 上述输入的预期输出应该是一个包含所有这些键(按任意顺序)的数组
output1 = ["g", "f", "e", "a.b.c", "a.d"]
output2 = ["a.b.c.h.j", "a.b.c.m.n", "a.d.k.l", "e", "f", "g"]
Run Code Online (Sandbox Code Playgroud) 我正在做一个练习来压缩嵌套列表.代码在控制台中工作,但它在文件中时不起作用.我不知道是怎么回事.:(
def flatten(nested):
"""
>>> flatten([2, 9, [2, 1, 13, 2], 8, [2, 6]])
[2, 9, 2, 1, 13, 2, 8, 2, 6]
>>> flatten([[9, [7, 1, 13, 2], 8], [7, 6]])
[9, 7, 1, 13, 2, 8, 7, 6]
>>> flatten([[9, [7, 1, 13, 2], 8], [2, 6]])
[9, 7, 1, 13, 2, 8, 2, 6]
>>> flatten([[5, [5, [1, 5], 5], 5], [5, 6]])
[5, 5, 1, 5, 5, 5, 5, 6]
"""
simple = []
for x …
Run Code Online (Sandbox Code Playgroud) 假设我有一个a
如下构建的列表:
a1<-list(1,2,list("X",10))
a2<-list("A",list("B"),"C")
a3<-list(list("D",list("E")),3.14)
a<-list(a1,a2,a3)
Run Code Online (Sandbox Code Playgroud)
我怎样才能a
变成这样:
list(1,2,list("X",10),"A",list("B"),"C",list("D",list("E")),3.14)
Run Code Online (Sandbox Code Playgroud)
我想,对于任意数量的有效的解决方案a1, a2, a3... an
不仅为,a1, a2
并a3
如上面的例子英寸
在Mathematica我会简单地使用Flatten[a,1]
,但是如何在R中做到这一点?
我想要展平一个看起来像这样的数组:
[{
"id": 0,
"text": "item 0"
}, {
"id": 1,
"items": [{
"id": 2,
"text": "item 2"
}, {
"id": 3,
"items": [{
"id": 4,
"text": "item 4"
}]
}]
}]
Run Code Online (Sandbox Code Playgroud)
进入这个
[{
"id": 0,
"text": "item 0"
}, {
"id": 2,
"text": "item 2"
}, {
"id": 4,
"text": "item 4"
}]
Run Code Online (Sandbox Code Playgroud)
基本上保留所有没有"items"属性的元素,如果它们有一个,则以递归方式遍历所有"items"数组.
我确实可以写一个递归函数,但我正在寻找一个漂亮的lodash或下划线方法来解决这个问题.
我合并了2个列表.
list1 = customer.links.where(ext: true).group(:link_id, :external).limit(100).order('count_id desc').count('id')
list2 = customer.links.where(ext: false).where.not(url: '/specific_link').group(:url, :ext).limit(100).order('count_id desc').count('id')
list = list1.merge(list2).sort_by{|k, v| -v}
Run Code Online (Sandbox Code Playgroud)
结果是:
[[["/path/element1", false], 7], [[4, true], 5], [["/path/element6", false], 1]]
Run Code Online (Sandbox Code Playgroud)
我想将它转换为on维哈希,如下所示:
[["/path/element1", false, 7], [4, true, 5], ["/path/element6", false, 1]]
Run Code Online (Sandbox Code Playgroud)
当我使用时flatten
,阵列之间没有分离.