如何从字典中的列表中删除元素

时间:2014-05-12 08:31:01

标签: python list dictionary

我有一个字典result,里面有列表。在其中一个列表中(" Failure"),其元素可以等于1,2或3.例如,元素号9等于3(或1),我想删除每个第九个元素在同一个字典中的四个列表("Failure","LoadLevel",""WeightedLevel" and "Blocks")中。我使用Python 3.4。

这是我的字典:

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 个答案:

答案 0 :(得分:2)

试试这个:

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