繁体   English   中英

从单个 python 列表中删除特殊字符

[英]Remove special characters from individual python list

我有一个包含许多元素的列表。 我能够找到一种方法来删除重复项、空白值和空格。

剩下的就是:

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

整个转换序列(不包括唯一性)可以通过列表推导来完成:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM