简体   繁体   English

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

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

I am making a text analyzer in python. 我正在用python创建文本分析器。 I am trying to remove any string that does not contain any letters or integers from that list. 我正在尝试从该列表中删除任何不包含任何字母或整数的字符串。 I am stuck and do not know how to do so. 我被卡住了,不知道该怎么做。 Currently when counting the length of my list it is including the string '-' and I do not want it to because i don't want to count this as a word. 目前,在计算列表的长度时,它包括字符串“-”,我不希望这样,因为我不想将此视为一个单词。 However I'd rather not use string.remove('-') because I want it to work for other inputs. 但是我宁愿不使用string.remove('-'),因为我希望它能用于其他输入。

Thanks in advance. 提前致谢。

I think what you mean is you want to filter out strings with no alphanumeric characters from a list of strings. 我认为您的意思是您要从字符串列表中过滤掉没有字母数字字符的字符串。 So ['a','b','*'] => ['a','b'] 所以['a','b','*'] => ['a','b']

Not too hard: 不是太难:

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

If you want to keep the strings with alphanumeric chars in them but that also contain non-alphanumeric chars: 如果要保留字符串中包含字母数字字符,但其中包含非字母数字字符:

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

Otherwise, to keep only the strings entirely populated by and only by alphanumeric chars: 否则,仅保留完全字母数字字符填充的字符串:

str.isalnum() is your man: str.isalnum()是你的男人:

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

More on re module: 有关模块的更多信息:

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

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

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

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