我有一个包含许多元素的列表。我找到了一种删除重复项、空白值和空白的方法。
唯一剩下的就是:
结果列表的顺序并不重要。最后一份清单只应包括:
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']发布于 2015-11-14 01:55:51
整个转换序列(不包括唯一性)可以通过一个列表理解来完成:
FinalList = [elem.strip() for elem in set(XYList) if elem and "." not in elem and "ae" not in elem]发布于 2015-11-14 01:57:19
filtered_l = [s for s in XYList if 'ae' not in s and '.' not in s]https://stackoverflow.com/questions/33704290
复制相似问题