我有一個列表中有幾個空列表。我按照這裏的指示(Python: How to remove empty lists from a list?)試圖過濾出空的列表,但失敗了,我真的沒有理由。Python - 從列表中刪除空列表
我的列表:
data: [['#0721', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['GBE COD', '746', '$2.00', '', '$1,492.00'], ['GBW COD', '13,894', '$0.50', '', '$6,947.00'], ['GOM COD', '60', '$2.00', '', '$120.00'], ['GB WINTER FLOUNDER', '94,158', '$0.25', '', '$23,539.50'], ['GOM WINTER FLOUNDER', '3,030', '$0.50', '', '$1,515.00'], ['GBE HADDOCK', '18,479', '$0.02', '', '$369.58'], ['GOM HADDOCK', '0', '$0.02', '', '$0.00'], ['GBW HADDOCK', '110,470', '$0.02', '', '$2,209.40'], ['HAKE', '259', '$1.30', '', '$336.70'], ['PLAICE', '3,738', '$0.40', '', '$1,495.20'], ['POLLOCK', '3,265', '$0.02', '', '$65.30'], ['WITCH FLOUNDER', '1,134', '$1.30', '', '$1,474.20'], ['SNE YT', '1,458', '$0.65', '', '$947.70'], ['GB YT', '4,499', '$0.70', '', '$3,149.30'], ['REDFISH', '841', '$0.02', '', '$16.82'], ['', '', '', '', ''], ['', '', '', '', ''], ['54 DAS @ $8.00/DAY = 432.00', '', ''], ['', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '']]
我的代碼:
data = filter(None, data)
print("second data:", data)
data = list(data)
print("third data:", data)
主要生產:
second data: <filter object at 0x05F23CF0>
third data: [['#0721', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['GBE COD', '746', '$2.00', '', '$1,492.00'], ['GBW COD', '13,894', '$0.50', '', '$6,947.00'], ['GOM COD', '60', '$2.00', '', '$120.00'], ['GB WINTER FLOUNDER', '94,158', '$0.25', '', '$23,539.50'], ['GOM WINTER FLOUNDER', '3,030', '$0.50', '', '$1,515.00'], ['GBE HADDOCK', '18,479', '$0.02', '', '$369.58'], ['GOM HADDOCK', '0', '$0.02', '', '$0.00'], ['GBW HADDOCK', '110,470', '$0.02', '', '$2,209.40'], ['HAKE', '259', '$1.30', '', '$336.70'], ['PLAICE', '3,738', '$0.40', '', '$1,495.20'], ['POLLOCK', '3,265', '$0.02', '', '$65.30'], ['WITCH FLOUNDER', '1,134', '$1.30', '', '$1,474.20'], ['SNE YT', '1,458', '$0.65', '', '$947.70'], ['GB YT', '4,499', '$0.70', '', '$3,149.30'], ['REDFISH', '841', '$0.02', '', '$16.82'], ['', '', '', '', ''], ['', '', '', '', ''], ['54 DAS @ $8.00/DAY = 432.00', '', ''], ['', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '']]
所以最終沒有做任何事情,這是爲什麼?
我這裏看不到任何空列表。一個包含5個空字符串的列表中仍然有5個東西。 – user2357112
你好。如果您想回答,請在下面添加 - 點擊「回答您的問題」。我們不會將解決方案作爲編輯添加到此處的問題中。 – halfer