关于python:如何从字典的列表中删除元素

How to delete elements from a list in a dictionary

本问题已经有最佳答案,请猛点这里访问。

我有一本字典,result,里面有目录。在其中一个列表中("失败"),其元素可以等于1、2或3。例如,如果元素9等于3(或1),我想删除同一字典中四个列表中的每个第九个元素("Failure","LoadLevel",""WeightedLevel" and"Blocks")。我使用的是Python3.4。

这是我的字典:

1
result =  {'LoadLevel': [0.212399, 0.393191, 0.727874, 1.347436, 2.494368, 4.617561, 8.548006, 15.824027, 29.293362, 54.227726, 1.0], 'WeightedLevel': [388.850952, 716.718689, 1312.55957, 2405.087158, 4460.083984, 8543.792969, 18805.201172, 57438.140625, 196024.515625, 860072.3125, 1792.367554], 'Blocks': [7255.151855, 231.589661, 9.365415, 0.55364, 0.0504, 0.006408, 0.001204, 0.000842, 0.909091, 1e-06, 2.060041], 'Failure': [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 2.0], 'Job': 'Desktop', 'Task': 'test'}


试试这个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
result =  {'LoadLevel': [0.212399, 0.393191, 0.727874, 1.347436, 2.494368, 4.617561, 8.548006, 15.824027, 29.293362, 54.227726, 1.0],
           'WeightedLevel': [388.850952, 716.718689, 1312.55957, 2405.087158, 4460.083984, 8543.792969, 18805.201172, 57438.140625, 196024.515625, 860072.3125, 1792.367554],
           'Blocks': [7255.151855, 231.589661, 9.365415, 0.55364, 0.0504, 0.006408, 0.001204, 0.000842, 0.909091, 1e-06, 2.060041],
           'Failure': [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 2.0],
           'Job': 'Desktop',
           'Task': 'test'}

if result['Failure'][8] == 1.0 or result['Failure'][8] == 3.0:
    del result['LoadLevel'][8]
    del result['WeightedLevel'][8]
    del result['Blocks'][8]
    del result['Failure'][8]

print result