我有一个包含许多元素的列表。 我能够找到一种方法来删除重复项,空白值和空格。
唯一剩下的就是:
结果列表的顺序并不重要。 最终列表应仅包含:
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 :(得分:1)
整个转换序列(不包括唯一性)可以通过列表理解来完成:
FinalList = [elem.strip() for elem in set(XYList) if elem and "." not in elem and "ae" not in elem]
答案 1 :(得分:0)
filtered_l = [s for s in XYList if 'ae' not in s and '.' not in s]