如何在Python中使用循环遍历字典列表并提取部分数值?

huangapple go评论63阅读模式
英文:

How can I iterate through a list of dictionaries and extract a subset of values in Python using loops?

问题

我有一个Python列表,如下所示:

data_list等于字典对象中的“values”键,该字典对象是一个项目列表

"values": [
    {
        "a": 1,
        "b": 2,
        "center code here": 3,
        "d": 4
    },
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4
    },
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4
    },
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4
    }
]

我还有另一个列表,其中包含我想要的值:

listx = ["a", "b"]

这是我尝试过的代码:

listx = ["a", "b"]
dict2 = {}

for d in data_list:
    for v, x in d.items():
        if v in listx:
            dict2[v] = x

print(len(dict2))

我得到的结果是12,这是一个字典中的项目数。它没有遍历其他字典。有人可以帮忙吗?

我的目标是能够创建一个具有以下值的列表子集:

"values": [
    {
        "a": 1,
        "b": 2
    },
    {
        "a": 1,
        "b": 2
    },
    {
        "a": 1,
        "b": 2
    },
    {
        "a": 1,
        "b": 2
    }
]

有人可以帮助我吗?我做错了什么?

英文:

Iterate through a list of dictionaries to get subset

I have a python list as follows:

data_list is equivalent to values key
in a dictionary object which is a list of items

 "values": [
        {
            "a": 1,
            "b": 2,
            "center code here": 3,
            "d": 4,
        },
        {
            "a": 1,
            "b": 2,
            "c": 3
            "d": 4,
        },
        {
            "a": 1,
            "b": 2,
            "c": 3
            "d": 4,
        },
        {
            "a": 1,
            "b": 2,
            "c": 3
            "d": 4,

        }
    ]

I also have another list that holds the values i want

listx = [a,b]

This is what i tried :

 listx = [a,b]
 dict2 = {}    
                
 for d in data_list:     
     for v,x in d.items(): 
          if v in listx:
              dict2[v] = (x)
                
 print(len(dict2))

I get 12 which is the number of items in one dictionary. It doesn't iterate over the other dictionaries?
Can anyone help ?

My goal is to be able to create a subset of this list with the following values :

     "values": [
        {
            "a": 1,
            "b": 2
        },
        {
            "a": 1,
            "b": 2
        },
        {
            "a": 1,
            "b": 2
        },
        {
            "a": 1,
            "b": 2

        }
    ]

Can anyone help me here ? What am i doping wrong?

答案1

得分: 3

以下是您要翻译的代码部分:

values = [
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
    },
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
    },
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
    },
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
    },
]

listx = ["a", "b"]

values_filtered = [{k: v for k, v in d.items() if k in listx} for d in values]
print(values_filtered)

输出:

[
 {"a": 1, "b": 2}, 
 {"a": 1, "b": 2}, 
 {"a": 1, "b": 2}, 
 {"a": 1, "b": 2}
]
英文:

You can use simple list comprehension to filter the values:

values = [
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
    },
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
    },
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
    },
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
    },
]

listx = ["a", "b"]

values_filtered = [{k: v for k, v in d.items() if k in listx} for d in values]
print(values_filtered)

Prints:

[
 {"a": 1, "b": 2}, 
 {"a": 1, "b": 2}, 
 {"a": 1, "b": 2}, 
 {"a": 1, "b": 2}
]

答案2

得分: 2

以下是您提供的Python代码的翻译部分:

listx = ["a", "b"]
subset = []

for d in data_list:
    subset_dict = {}
    for key, value in d.items():
        if key in listx:
            subset_dict[key] = value
    subset.append(subset_dict)

print(subset)

输出:

[{'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {'a': 1, 'b': 2}]

或者您可以使用itemgetter

from operator import itemgetter

listx = ["a", "b"]
subset = [dict(zip(listx, itemgetter(*listx)(d))) for d in data_list]
print(subset)
英文:
listx = ["a", "b"]
subset = []

for d in data_list:
    subset_dict = {}
    for key, value in d.items():
        if key in listx:
            subset_dict[key] = value
    subset.append(subset_dict)

print(subset)

Output:

[{'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {'a': 1, 'b': 2}]

Or you can use itemgetter:

from operator import itemgetter

listx = ["a", "b"]
subset = [dict(zip(listx, itemgetter(*listx)(d))) for d in data_list]
print(subset)

huangapple
  • 本文由 发表于 2023年6月1日 05:08:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定