2017-09-19 54 views
2
lijst = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], 
     [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], 
     [], [], [], [], [], [], [], [], [], [], [], [], 
     ['/vacatures/oracle-plsql-ontwikkelaar-1/'], [], [], [], [], 
     ['/vacatures/oracle-plsql-ontwikkelaar-1/'], 
     ['/vacatures/business-intelligence-developer-1/'], [], [], [], [], [], 
     ['/vacatures/business-intelligence-developer-1/'], 
     ['/vacatures/oracle-dba/'], [], [], ['/vacatures/oracle-dba/'], 
     ['/vacatures/database-beheerder/'], [], [], [], 
     ['/vacatures/database-beheerder/'], 
     ['/vacatures/sql-server-dba-powershell/'], [], [], [], 
     ['/vacatures/sql-server-dba-powershell/'], 
     ['/vacatures/junior-msbi-consultant/'], [], [], [], [], [], 
     ['/vacatures/junior-msbi-consultant/'], 
     ['/vacatures/senior-msbi-consultant/'], [], [], [], [], [], 
     ['/vacatures/senior-msbi-consultant/'], 
     ['/vacatures/medior-msbi-consultant/'], [], [], [], [], 
     ['/vacatures/medior-msbi-consultant/'], 
     ['/vacatures/zos-mainframe-specialist/'], [], [], 
     ['/vacatures/zos-mainframe-specialist/'], 
     ['/vacatures/junior-business-analyst/'], [], [], [], [], 
     ['/vacatures/junior-business-analyst/'], [], [], [], [], [], [], [], 
     [], ['/vacatures/oracle-plsql-ontwikkelaar-1/'], [], [], 
     ['/vacatures/oracle-dba/'], [], [], 
     ['/vacatures/business-intelligence-developer-1/'], [], [], 
     ['/vacatures/database-beheerder/'], [], [], 
     ['/vacatures/sql-server-dba-powershell/'], [], [], [], [], [], [], [], 
     [], [], []] 

我有一個問題。如何過濾掉空列表並刪除2維列表中的重複項?2維列表排序蟒森蚺3.6.1

+0

應該是什麼輸出? –

+0

@EricDuminil如果OP想讓它變平? – Kanak

+0

你一直很樂意獲得關於SO的答案嗎?考慮閱讀[當某人回答我的問題時我應該怎麼做?](https://stackoverflow.com/help/someone-answers)。 – Kanak

回答

3

這是因爲這樣做

new_list0 = list(filter(len, lijst)) 

,然後刪除重複的那樣簡單,你可以把new_listset,然後將它轉換回一個列表。具體如下

new_list1 = list(set(tuple(x) for x in new_list0)) 

如果你想投的new_list1的元素(即是元組現在)回列表,有些東西你可以做的是

new_list2 = list(map(list, new_list1)) 


但是,還給數量(鑄造發電機,列表,設置,...,列表,等等), 出現更好的性能方面可能是

new_list = [] 
for el in lijst: 
    if el and el not in new_list: 
     new_list.append(el)    
#print(new_list) 

最後,請注意,new_list仍然是2維的,就像原來的那樣。如果你想讓它1維的,是你可以做的是making it flat,如下

import itertools 
new_list = list(itertools.chain.from_iterable(new_list)) 

或者直接創建它作爲一個一維列表,並通過避免減少的時間複雜度O(n)(而不是O(n**2)in操作者)

new_set = set() 
for el in lijst: 
    if el: 
     new_set.update(el)   
new_list = list(new_set) 


答案測試和功能

+0

這將導致'TypeError:不可用類型:'list''。 – andrewlamb

+0

看起來不錯。如果有必要,你可以將內部元組轉換回列表。 – andrewlamb

+0

您的最後一個示例不起作用。 –

2

你的名單並不是二維的。每個列表都有0或1個元素。

在這種情況下,你可以只提取字符串,把它們放在一個組:

print({l[0] for l in lijst if l}) 

它輸出:

set(['/vacatures/junior-msbi-consultant/', '/vacatures/junior-business-analyst/', '/vacatures/business-intelligence-developer-1/', '/vacatures/zos-mainframe-specialist/', '/vacatures/sql-server-dba-powershell/', '/vacatures/database-beheerder/', '/vacatures/medior-msbi-consultant/', '/vacatures/oracle-dba/', '/vacatures/oracle-plsql-ontwikkelaar-1/', '/vacatures/senior-msbi-consultant/']) 

它的簡潔和快速(O(n))。

+0

明顯比我的回答好。但是如果OP想要將最終輸出保持爲(不是真正的)2d列表,那麼它不起作用,但這不太可能。 – Kanak