从词典字典中删除空字典

时间:2019-05-09 06:23:57

标签: python python-3.x dictionary

我有字典A:

A =  {"('All', 'Delhi', 'Mumbai')": {}, "('Container', 'Delhi', 'Mumbai')": {}, 
      "('Open', 'Delhi', 'Mumbai')": {12: [12, 22, 25], 7: [9, 5]},
      "('Open', 'Doon', 'Gurgaon')": {10: [1, 2, 24], 8: [4], 9: [28, 8], 7:[21]}}

我想删除所有空字典,该怎么办?

2 个答案:

答案 0 :(得分:1)

使用理解力:

>>> {k:v for k,v  in A.items() if v}
{"('Open', 'Delhi', 'Mumbai')": {12: [12, 22, 25], 7: [9, 5]}, "('Open', 'Doon', 'Gurgaon')": {10: [1, 2, 24], 8: [4], 9: [28, 8], 7: [21]}}

答案 1 :(得分:0)

您可以在这里使用一种理解:

var range = worksheet.Range("B3:L13");
var objArray = new object[range.ColumnCount(), range.RowCount()];
for (int i = 0; i < range.ColumnCount(); i++)
{
    for (int j = 0; j < range.RowCount(); j++)
    {
        objArray[i, j] = range.Cell(j+1, i+1).Value;
    }
}

请参见a demo on ideone.com