繁体   English   中英

从python的字符串列表中删除不包含字母的字符串

[英]Removing a string that does not contain letters from a list of strings in python

我正在用python创建文本分析器。 我正在尝试从该列表中删除任何不包含任何字母或整数的字符串。 我被卡住了,不知道该怎么做。 目前,在计算列表的长度时,它包括字符串“-”,我不希望这样,因为我不想将此视为一个单词。 但是我宁愿不使用string.remove('-'),因为我希望它能用于其他输入。

提前致谢。

我认为您的意思是您要从字符串列表中过滤掉没有字母数字字符的字符串。 所以['a','b','*'] => ['a','b']

不是太难:

In [39]: l = ['adsfg','sdfgb','gdc','56hjfg1','&#$%^',"asfgd3$#$%^" ]
In [40]: l = filter (lambda s:any([c.isalnum() for c in s]), l)
Out[41]:  ['adsfg', 'sdfgb', 'gdc', '56hjfg1', 'asfgd3$#$%^']

In [42]: 

如果要保留字符串中包含字母数字字符,但其中包含非字母数字字符:

import re

strings = ["string", "&*()£", "$^TY?", "12345", "2wE4T", "@#~\!", "^(*4"]

strings = [s for s in strings if re.search(r'\w+', s)] #  \w matches alphanumeric chars

print strings
['string', '$^TY?', '12345', '2wE4T', '^(*4'] # now we can work with these wanted strings

否则,仅保留完全字母数字字符填充的字符串:

str.isalnum()是你的男人:

strings = [s for s in strings if s.isalnum()]
print strings
['string', '12345', '2wE4T']

有关模块的更多信息:

https://docs.python.org/2/howto/regex.html

http://www.regular-expressions.info/tutorial.html

暂无
暂无

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

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