【问题标题】:How to remove all keys that have value of N/A, -, or empty strings appear in an array如何删除数组中出现的所有值为 N/A、- 或空字符串的键
【发布时间】:2022-08-23 16:52:47
【问题描述】:

如何使用 python 删除空字典。

{\'name\': {\'first\': \'Robert\', \'middle\': \'\', \'last\': \'Smith\'}, \'age\': 25、\'DOB\':\'-\',\'爱好\':[\'跑步\',\'编码\',\'-\'],\'教育\':{\'高中\': \'N/A\', \'大学\': \'耶鲁\'}}

  • 而 dictList 中的 {}:dictList.remove({})
  • 这将做 dictList.clear()
  • 你的输出应该是什么样子?

标签: python json dictionary


【解决方案1】:

正如一些人在 cmets 中已经建议的那样,做一个dictList.clear就可以了。

你也可以做一个while循环并搜索任何空的dict(你不是在搜索'dict',而是在搜索值/键对)。

while {} in YourDictList:
    YourDictList.clear()

或者

while {} in YourDictList:
    YourDictList.clear({})

两者都有效。如果您已经知道要搜索的键或值或者您知道是空的,您当然也可以通过进入 dict 的值并更改/删除键来手动删除它

【讨论】:

    猜你喜欢
    • 2021-05-09
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2012-12-06
    相关资源
    最近更新 更多