简体   繁体   English

将字典转换为具有相同键、值和布局的列表

[英]Convert dict to list with same keys, values and layout

I´m trying to extract several keys/values out of a List.我正在尝试从列表中提取几个键/值。

My List:我的清单:

a = [ 
      {
        "id": "1",
        "system": "2",
      },
      {
        "id": "3",
        "system": "4",
      }
    ]

Now i need to parse this into a function ( next function) and it returns a[current] or a[0] .现在我需要将其解析为一个函数(下一个函数)并返回a[current]a[0] But now is a[current] or a[0] a dict.但是现在是a[current]a[0]一个字典。

Next step is just to extract the ID and the value of it.下一步只是提取ID和它的值。 But this below only works if a is a list!但这仅在a是列表时才有效! So i need to convert the a[current] or a[0] into a list.所以我需要将a[current]a[0]转换成一个列表。 The code below has to be the same because it´sa function and if i cannot change this for several reasons, so i need to convert the dict a into a list a .下面的代码必须相同,因为它是一个函数,如果由于多种原因我无法更改它,那么我需要将 dict a转换为列表a

c = list()
for data in a:
     value = dict()
     value["id"] = data.get("id")
     c.append(value)

And here i stuck, i tried several methods like .keys(), .values(), but i can´t put them together to a list anymore.我在这里卡住了,我尝试了几种方法,如 .keys()、.values(),但我不能再将它们放在一起到列表中。 It needs to be scaleable/configurable because a changes from time to time (not a[0]["id"], ...).它需要可扩展/可配置,因为a会不时发生变化(不是 a[0]["id"], ...)。 Currently a[0] looks like this: {'id': '1', 'system': '2'}, but it needs to be like this: [{'id': '1', 'system': '2'},], that i can parse it to my search function.目前 a[0] 看起来像这样:{'id': '1', 'system': '2'},但它需要像这样: [{'id': '1', 'system': ' 2'},],我可以将其解析为我的搜索功能。

I need a new list like c :我需要一个像c这样的新列表:

    c = [ 
          {
            "id": "1",
          },
          {
            "id": "3",
          }
        ]

code updated:代码更新:

a = [ 
      {
        "id": "1",
        "system": "2",
      },
      {
        "id": "3",
        "system": "4",
      }
    ]
    
print([[value] for value in a ])

Result:结果:

[[{'id': '1', 'system': '2'}], [{'id': '3', 'system': '4'}]]

Is this your your expected output:这是您的预期输出:

a = [ 
      {
        "id": "1",
        "system": "2",
      },
      {
        "id": "3",
        "system": "4",
      }
    ]
    
c = list()
for data in a:
     value={}
     value["id"]=data.get("id")
     c.append([value])
     # c.extend([value])

print(c)
# [[{'id': '1'}], [{'id': '3'}]]

# print(c) # Extend's output 
# [{'id': '1'},{'id': '3'}]

Or you can try one-line solution或者您可以尝试单线解决方案

print([[{"id":val.get("id")}] for val in a])
# [[{'id': '1'}], [{'id': '3'}]]

Or as your comment if you just want或者如果你只是想作为你的评论

[{'id': '1', 'system': '2'},]

Then :然后 :

print([a[0]])

Here is a function to filter your dicts list:这是一个过滤字典列表的函数:

def filter_dicts(key, a):
    return [{key: element[key]} for element in a]

Use it like this:像这样使用它:

c = filter_dicts("id", a)

Note that this will cause an error if there is a dict without the specified key, which may or may not be what you want.请注意,如果存在没有指定键的字典,这将导致错误,这可能是您想要的,也可能不是。 To avoid this, replace element[key] with element.get(key, None) .为避免这种情况,请将element[key]替换为element.get(key, None)

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

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