简体   繁体   English

删除字典中的键并将值存储在列表的字典中

[英]Remove Keys in Dictionary and Store Values in a Dictionary of Lists

So I have a dictionary with "items" which has a list of dictionaries. 因此,我有一本带有“项目”的字典,其中包含字典列表。 I'm trying to restructure it to be a dictionary of "items" that has a list of lists that contain the values of the previous dictionary's keys. 我正在尝试将其重组为“项”的字典,该字典具有包含上一本字典键的值的列表列表。

Original: 原版的:

data = { 
   "items": [ 
           { "A": 0.00, "B": 33.27, "C": "string", "D": "16122 " }, 
           { "A": 0.00, "B": 5176.66, "C": "string", "D": "21216 " } 
            ] 
       }

What I want to get: 我想得到什么:

data = { 
    "items": [ 
           [ 0.00, 33.27, "string", "16122 " ], 
           [ 0.00, 5176.66, "string", "21216 " ] 
             ] 
         }

It seems like operator.itemgetter is almost what you want: 似乎operator.itemgetter几乎是您想要的:

getter = operator.itemgetter('A', 'B', 'C', 'D')
data = {'items': [getter(dct) for dct in data['items']]}

In this case you end up with a list of tuple , not a list of list , but in many applications, that's probably OK. 在这种情况下,你最终有一个listtuple ,而不是一个listlist ,但在许多应用中,这可能是确定。

Demo: 演示:

>>> data = { 
...    "items": [ 
...            { "A": 0.00, "B": 2184.83, "C": "string", "D": "16122 " }, 
...            { "A": 0.00, "B": 5176.66, "C": "string", "D": "21216 " } 
...             ] 
...        }
>>> import operator
>>> getter = operator.itemgetter('A', 'B', 'C', 'D')
>>> data = {'items': [getter(dct) for dct in data['items']]}
>>> data['items'][0]
(0.0, 2184.83, 'string', '16122 ')
>>> data['items'][1]
(0.0, 5176.66, 'string', '21216 ')

Here is one way to do exactly the way you wanted. 这是一种完全按照您想要的方式进行操作的方法。

#Get the column names from the first record
colNames =data['items'][0].keys()
#Get values from all records that have the same keys as in the first record
newData = { 'items' : [[record[colName] for colName in colNames] \
                   for record in data['items']] }
print newData

output: 输出:

{'items': [[0.0, 'string', 33.27, '16122 '], [0.0, 'string', 5176.66, '21216 ']]}

Keep in mind that dicts are unordered -- therefore, you need to specify the order of the keys to get a correlated order of the values when mapped to a list. 请记住,字典是无序的-因此,当映射到列表时,需要指定键的顺序以获得值的相关顺序。 The order of the keys will not necessarily be the order that they are declared, the order they were last time you looked, etc. 键的顺序不一定是声明的顺序,上次查看的顺序等等。

So a more realistic example data is: 因此,更实际的示例数据是:

data = { 
   "items": [ 
           { "D": "16122 ", "A": 0.00, "B": 33.27, "C": "string" }, 
           { "B": 5176.66, "A": 0.00,  "D": "21216 ", "C": "string" } 
            ] 
       }

To map unordered keys into an ordered list, you need to pick what order you will use. 要将无序键映射到有序列表,您需要选择要使用的顺序。 Suppose you settle on the ascii betical order as the order: 假设您按照以下顺序排列顺序:

ordered_keys=("A", "B", "C", "D")    

Then you can convert to your structure with a simple loop: 然后,您可以通过简单的循环转换为结构:

for k, LoD in data.items():      # consider '.iteritems() on Py 2 and larger dicts...
    data[k]=[[di[sk] for sk in ordered_keys] for di in LoD]

>>> data
{'items': [[0.0, 33.27, 'string', '16122 '], [0.0, 5176.66, 'string', '21216 ']]}

Now you need to decide what to do with keys that may be missing in the list of dicts. 现在,您需要决定如何处理字典列表中可能缺少的键。 Unless each dict has exactly the same keys, you need a default value. 除非每个字典具有完全相同的键,否则您需要一个默认值。

Here is a way you could do that: 这是您可以执行此操作的方法:

data = { 
   "items": [ 
           { "D": "16122 ", "A": 0.00, "B": 33.27, "C": "string" }, 
           { "B": 5176.66, "A": 0.00,  "D": "21216 ", "C": "string" }, 
           {  "E": "New Key ", "C": "'A' and 'B' are missing in this dict" } 
            ] 
       }

for k, LoD in data.items():     
    keys=sorted({e for sk in LoD for e in sk})
    data[k]=[keys]+[[di.get(sk, None) for sk in keys] for di in LoD]

In this case, all the keys in the list of dicts are gathered, sorted, then made the first element in the list of lists (so you know which is which and other keys of data may have different set of keys.): 在这种情况下,字典列表中的所有键均被收集,排序,然后成为列表列表中的第一个元素(因此您知道哪个data键和其他data键可能具有不同的键集。):

data = { 
   "items": [ 
           { "D": "16122 ", "A": 0.00, "B": 33.27, "C": "string" }, 
           { "B": 5176.66, "A": 0.00,  "D": "21216 ", "C": "string" }, 
           {  "E": "New Key ", "C": "'A' and 'B' are missing in this dict" } 
            ],
    "More": [
           { "D": "16122 ", "A": 0.00, "B": 33.27, "C": "string" }
            ]             
       }

for k, LoD in data.items():     
    keys=sorted({e for sk in LoD for e in sk})
    data[k]=[keys]+[[di.get(sk, None) for sk in keys] for di in LoD]

Result: 结果:

>>> for k in data:
...     print k+':'+'\n\t'+'\n\t'.join(repr(e) for e in data[k])
items:
    ['A', 'B', 'C', 'D', 'E']
    [0.0, 33.27, 'string', '16122 ', None]
    [0.0, 5176.66, 'string', '21216 ', None]
    [None, None, "'A' and 'B' are missing in this dict", None, 'New Key ']
More:
    ['A', 'B', 'C', 'D']
    [0.0, 33.27, 'string', '16122 ']

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

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