2015-11-14 29 views
2

我有一個包含很多元素的列表。 我能夠找到一種方法來刪除重複項,空白值和空白。從隱形python列表中刪除特殊字符

剩下的唯一一件事就是:

  1. 刪除任何東西,包括(AE)的字符串。
  2. 從列表中刪除包含期內的任何東西(。)

結果列表的順序並不重要。 的最終列表應該只包含:

FinalList = ['eth-1/1/0', 'jh-3/0/1', 'eth-5/0/0','jh-5/9/9'] 

代碼:

XYList = ['eth-1/1/0', 'ae1', 'eth-1/1/0', 'eth-1/1/0', 'ae1', 'jh-3/0/1','jh-5/9/9', 'jh-3/0/1.3321', 'jh-3/0/1.53', 'ae0', '', 'eth-5/0/0', 'ae0', '', 'eth-5/0/0', 'ae0', 'eth-5/0/0', '', 'jh-2.1.2'] 
XYUnique = set(XYList) 
XYNoBlanks = (filter(None,XY)) 
RemovedWhitespace = [item.strip() for item in XYNoBlanks] 
# the order of the list is not important 
# the final result should be 

FinalList = ['eth-1/1/0', 'jh-3/0/1', 'eth-5/0/0','jh-5/9/9'] 
+0

'列表(如果s和'ae'不在s中且'''不在s中],則設置[[對於XY列表中的s,s])) ' –

回答

1

整個轉換序列(不包括唯一性)可與列表解析來完成:

FinalList = [elem.strip() for elem in set(XYList) if elem and "." not in elem and "ae" not in elem] 
0
filtered_l = [s for s in XYList if 'ae' not in s and '.' not in s] 
+0

你有沒有檢查你的代碼? –

+0

'list(set([s for XYList如果s和'ae'不在s中並'''不在s中])) ' –

+0

是的,這也適用。 –