Python 如果匹配密钥,则删除整个dict

Python 如果匹配密钥,则删除整个dict,python,dictionary,scripting,Python,Dictionary,Scripting,我不熟悉python,但仍然尝试在internet上搜索解决方案。但没有找到任何解决办法。以下是我要整理的数据: {'Items': [ {'event': 'active', 'options.target': 'items', 'meta.timestamp': 1584826254819, 'id': 'foo111', 'userId': '1a938ea1-c65e-641a-5db5-5e56c7d8e90b', 'properties.new': 'ABC123'

我不熟悉python,但仍然尝试在internet上搜索解决方案。但没有找到任何解决办法。以下是我要整理的数据:

{'Items': [
        {'event': 'active', 'options.target': 'items', 'meta.timestamp': 1584826254819, 'id': 'foo111', 'userId': '1a938ea1-c65e-641a-5db5-5e56c7d8e90b', 'properties.new': 'ABC123'
        },
        {'event': 'active', 'options.target': 'previousTop', 'meta.timestamp': 1584820823598, 'id': 'foo111', 'userId': '1a938ea1-c65e-641a-5db5-5e56c7d8e90b', 'properties.new': 'KUCC00302'
        },
        {'_.from': 'engineEnd', '_.called': 'track', 'event': 'active', 'traits.id': 'foo333', 'options.target': 'items', 'properties.old': 'ABC123', 'meta.timestamp': 1584826518511, 'partner_resid': 'r-1a938ea1-c65e-641a-5db5-5e56c7d8e90b', 'id': 'foo111', 'userId': '1a938ea1-c65e-641a-5db5-5e56c7d8e90b', 'properties.new': 'ABC111', 'id': 'dc3775e0-6bbb-11ea-a789-4f693944a864', 'anonymousId': '3bd5eda3-8625-4b54-b02b-e24f78011ca7', '_.originalAction': 'track', 'type': 'track'}
}
我想修剪具有键的dictproperties.old 以下是我未能筛选的代码:

for item in json_result['Items']:
    if 'properties.old' in item:
        del item

关于我做错了什么有什么建议吗?

嘿,你失败了,因为你删除的是循环中的局部项变量,而不是列表元素。尝试在索引上迭代并使用pop或remove

        > >>> a=list(range(10))
        > >>> for i in a:       
                  del i    
        > >>> a 
          [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        > >>>

<>你可以考虑创建一个列表理解的新对象。 看看这一行:

result = { 'Items' : [dic for dic in json_result['Items'] if not 'properties.old' in dic] }
这应该适用于您(您只需要遍历内部字典的键):

产出:

{'Items': [{'event': 'active', 'options.target': 'items', 'meta.timestamp': 1584826254819, 'id': 'foo111', 'userId': '1a938ea1-c65e-641a-5db5-5e56c7d8e90b', 'properties.new': 'ABC123'}, {'event': 'active', 'options.target': 'previousTop', 'meta.timestamp': 1584820823598, 'id': 'foo111', 'userId': '1a938ea1-c65e-641a-5db5-5e56c7d8e90b', 'properties.new': 'KUCC00302'}]}

首先获取索引,并在实际删除之前进行试运行(打印结果):

from pprint import pprint as pp

json_result = {'Items': [{'event': 'active',
                          'options.target': 'items',
                          'meta.timestamp': 1584826254819,
                          'id': 'foo111',
                          'userId': '1a938ea1-c65e-641a-5db5-5e56c7d8e90b',
                          'properties.new': 'ABC123'
                         },
                         {'event': 'active',
                          'options.target': 'previousTop',
                          'meta.timestamp': 1584820823598,
                          'id': 'foo111',
                          'userId': '1a938ea1-c65e-641a-5db5-5e56c7d8e90b',
                          'properties.new': 'KUCC00302'
                         },
                         {'_.from': 'engineEnd',
                          '_.called': 'track',
                          'event': 'active',
                          'traits.id': 'foo333',
                          'options.target': 'items',
                          'properties.old': 'ABC123',
                          'meta.timestamp': 1584826518511,
                          'partner_resid': 'r-1a938ea1-c65e-641a-5db5-5e56c7d8e90b',
                          'id': 'foo111',
                          'userId': '1a938ea1-c65e-641a-5db5-5e56c7d8e90b',
                          'properties.new': 'ABC111',
                          'id': 'dc3775e0-6bbb-11ea-a789-4f693944a864',
                          'anonymousId': '3bd5eda3-8625-4b54-b02b-e24f78011ca7',
                          '_.originalAction': 'track',
                          'type': 'track'
                         }
                        ]
               }

for item in json_result['Items']:
    if 'properties.old' in item:
        # pp(item)
        index = json_result['Items'].index(item)
        remove_item = json_result['Items'][index]
        # pp(remove_item)
        print(json_result['Items'][index]['properties.old'])
        del json_result['Items'][index]
返回:

ABC123

现在已删除该dict的词典条目的值。

感谢您的快速建议。我尝试了这种逻辑,但它并没有删除具有键属性的内部dict。old@SamudralaPrasad你完全正确,我在删除列表元素时犯了一个错误,现在它应该可以工作了,你可以试试。是的,甚至这个逻辑也像champ一样工作。谢谢你的帮助。:)这就像冠军一样。。。!!!!!您是Python方面的专家。谢谢。有没有办法用meta.timestamp对这些dict进行排序?是的,你可以使用sorted命令,并通过lambda函数表示排序,例如:{'Items':sorted([dic for dic in json_result['Items']if not'properties.old'in dic],key=lambda x:x['meta.timestamp'],reverse=False)}如果你想要颠倒顺序,请更改reverse=True。太好了。。。非常感谢。
ABC123