2017-06-01 259 views
-4

我有一個列表中有幾個空列表。我按照這裏的指示(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', '', ''], ['', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '']] 

所以最終沒有做任何事情,這是爲什麼?

+1

我這裏看不到任何空列表。一個包含5個空字符串的列表中仍然有5個東西。 – user2357112

+0

你好。如果您想回答,請在下面添加 - 點擊「回答您的問題」。我們不會將解決方案作爲編輯添加到此處的問題中。 – halfer

回答

2

如果a_list的任何元素評估爲True,您可以使用any(a_list),即True,因爲非空字符串。

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', '', ''], ['', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '']] 

non_empty = [sublist for sublist in data if any(sublist)] 
print(non_empty) 

[['#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', '', '']] 

 
+0

這可能是一個愚蠢的問題,但在你的'non_empty'行中,哪些是'1'(''')和哪個是'L'? – theprowler

+1

@theprowler我改了名字。 –

+0

哦,好吧,很酷,它完美的作品,謝謝一噸! – theprowler

0

由於user2357112指出你沒有空列表,你有字符串''的列表不是空列表。

要解決你的問題,你應該改變你傳遞給過濾器的第一個參數。過濾器(函數,可迭代)將返回一個列表,其中包含來自iterable的值,函數返回true。

在您的情況,請嘗試:

def isEmpty(list): 
    return any(list) 

print filter(isEmpty, data)