简体   繁体   English

从python dicts列表中取2个键值并制作新的list / tuple / array / dictionary,每个索引包含来自第一个列出的dict的2个键值

[英]Take 2 key values from list of python dicts & make new list/tuple/array/dictionary with each index containing 2 key values from 1st listed dict

I have a list of dictionaries in a json file. 我在json文件中有一个字典列表。

I have iterated through the list and each dictionary to obtain two specific key:value pairs from each dictionary for each element. 我已遍历列表和每个字典以获取每个元素的每个字典中的两个特定键:值对。 ie List[dictionary{i(key_x:value_x, key_y:value_y)}] List[dictionary{i(key_x:value_x, key_y:value_y)}]

My question is now: 我现在的问题是:

How do I place these two new key: value pairs in a new list/dictionary/array/tuple, representing the two key: value pairs extracted for each listed element in the original? 如何将这两个新的键:值对放在新的列表/字典/数组/元组中,表示为原始中每个列出的元素提取的两个键:值对?

To be clear: 要明确:

ORIGINAL_LIST      (i.e. with each element being a nested dictionary) = 
[{"a":{"blah":"blah",
      "key_1":value_a1,
      "key_2":value_a2,
      "key_3":value_a3,
      "key_4":value_a4,
      "key_5":value_a5,},
  "b":"something_a"},
 {"a":{"blah":"blah",
      "key_1":value_b1,
      "key_2":value_b2,
      "key_3":value_b3,
      "key_4":value_b4,
      "key_5":value_b5,},
  "b":"something_b"}]

So my code so far is: 所以到目前为止我的代码是:

    import json
    from collections import *
    from pprint import pprint

    json_file = "/some/path/to/json/file"

    with open(json_file) as json_data:
        data = json.load(json_data)
    json_data.close()

    for i in data:
        event = dict(i)
        event_key_b = event.get('b')
        event_key_2 = event.get('key_2')
        print(event_key_b)#print value of "b" for each nested dict for 'i'
        print(event_key_2)#print value of "key_2" for each nested dict for 'i'

To be clear: 要明确:

FINAL_LIST(i.e. with each element being a nested dictionary) = 

    [{"b":"something_a", "key_2":value_2},
     {"b":"something_b", "key_2":value_2}]

So I have an answer to getting the keys into individual dictionaries, as follows in the code below. 所以我有一个答案,将密钥放入单独的字典中,如下面的代码所示。 The only problem is that the value for 'key_2' in the original json dictionaries is either an int value or it is "" for values which are 0. My script just returns 'None' for all instances of value_2 for key_2. 唯一的问题是原始json字典中'key_2'的值是int值,或者对于0的值是“”。对于key_2的所有value_2实例,我的脚本只返回'None'。 How can I get it to read the appropriate values for 'value_2'? 如何让它读取'value_2'的相应值? I want to only return dictionaries for cases where 'value_2' > 0 (ie where value_2 != "") 我想只返回'value_2'> 0(即value_2!=“”)的情况下的词典

Below is the current code: 以下是当前代码:

import json
from pprint import pprint

json_file = "/some/path/to/json/file"

with open(json_file) as json_data:
    data = json.load(json_data)
json_data.close()

for i in data:
    event_key_b = event.get('b')
    for x in i:
        event_key_2 = event.get('key_2')
        x = {'b' : something_b, 'key_2' : value_2}
    print(x)

Also, if there are any more elegant solutions anyone can think of I would really be interested in learning them ... Some of the json files I'm looking at can range from 200 dictionary entries in the original list to 2,000,000. 此外,如果有任何更优雅的解决方案,任何人都可以想到我真的有兴趣学习它们...我正在看的一些json文件可以从原始列表中的200个字典条目到2,000,000个。 I'm planning to feed my parsed results into a message queue for processing by a different service and any efficiencies in the code will help for scalability in processing. 我打算将解析后的结果提供给消息队列,以便由不同的服务进行处理,代码中的任何效率都有助于提高处理的可扩展性。 Also if anyone has any recommendations to give on Redis vs. RabbitMQ, I'd really appreciate it 如果有人对Redis vs. RabbitMQ有任何建议,我真的很感激

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

相关问题 将列表中的值添加到字典列表中(作为每个字典中的新键值) - Add values from list to a list of dicts (as new key-val in each dict) 从字典拆分字典键和值列表 - Split dictionary key and list of values from dict 使用将字典中的字典存储在字典列表中的python API…没有键值 - Using a python api that stores a dict in a list of dicts…with no key values 从字典列表中提取特定键的值 - Extracting values for a specific key from a list of dicts 按某个键的值对列表中的字典进行分组 - Group dicts from a list by the values of a certain key 用于每个键的多个值的Python列表 - Python list to dict with multiple values for each key Python 从字典列表中搜索键返回值 - Python searching key returning values from list of dicts 如果与列表中每个字典中的一个键的值匹配,则将具有不同词典值的新键添加到词典列表中 - Add new key to list of dictionaries with the values from a different dictionaries if matched with value of one key in each dictionary in a list Python 字典,取一本字典并返回一个字典,其中键是前字典中的值之一,值是键 - Python dicts, take a dictionary and return a dict where the keys are one of the values from the former dict and the values are the keys Python:使用[0] =键和[1:] =值从列表创建字典 - Python: Create Dictionary From List with [0] = Key and [1:]= Values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM