如何从列表中删除一个特定元素

时间:2019-03-12 11:36:00

标签: python

我的变量L1看起来像这样:

[{'distance': 1.9999,
  'breakEvenDistance': 1.9329,
  'max': 0.0010342833053929787},
 {'distance': 1.9251,
  'breakEvenDistance': 2.0669999999999997,
  'max': 0.0011183923895419084,
  'min': 0.0010342833053929787},
 {'distance': 1.8802,
  'breakEvenDistance': 1.6758,
  'max': 0.0011927892918825562,
  'min': 0.0011183923895419084},
 {'distance': 1.8802,
  'breakEvenDistance': 1.5956,
  'max': 0.0012522046577102665,
  'min': 0.0011927892918825562}]

我需要删除列表中的最后一个'max'!我试图检查文档,在del上使用了功能L1[-1]['max'],但没有用..有任何想法或线索吗?谢谢!

1 个答案:

答案 0 :(得分:2)

-1获取dict的最后一个元素:

del dict_[-1]['max']
print(dict_)

输出:

[
 {'distance': 1.9999, 'breakEvenDistance': 1.9329, 'max': 0.0010342833053929787},
 {'distance': 1.9251, 'breakEvenDistance': 2.0669999999999997, 'max': 0.0011183923895419084, 
  'min': 0.0010342833053929787}, 
 {'distance': 1.8802, 'breakEvenDistance': 1.6758, 'max': 0.0011927892918825562, 
  'min': 0.0011183923895419084}, 
 {'distance': 1.8802, 'breakEvenDistance': 1.5956, 
  'min': 0.0011927892918825562}
]