Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从python中的字符串列表中删除不包含字母的字符串_Python_String_Python 3.4 - Fatal编程技术网

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

从python中的字符串列表中删除不包含字母的字符串,python,string,python-3.4,Python,String,Python 3.4,我正在用python制作一个文本分析器。我试图从列表中删除任何不包含任何字母或整数的字符串。我被卡住了,不知道怎么做。目前,当计算我的列表长度时,它包含字符串“-”,我不希望它包含在内,因为我不想将其计算为一个单词。但是,我不希望使用string.remove“-”,因为我希望它能用于其他输入 提前谢谢。我想你的意思是想从字符串列表中筛选出没有字母数字字符的字符串。所以['a','b','*']=>a','b'] 不太难: In [39]: l = ['adsfg','sdfgb','gdc',

我正在用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]: 

我想你的意思是想从字符串列表中筛选出没有字母数字字符的字符串。所以['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
否则,要仅保留完全由字母数字字符填充的字符串,请执行以下操作:

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']
有关re模块的更多信息:


如果要保留包含字母数字字符但也包含非字母数字字符的字符串:

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
否则,要仅保留完全由字母数字字符填充的字符串,请执行以下操作:

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']
有关re模块的更多信息:


你可能想使用re-Suggest也发布一些你尝试过的代码示例,等等你可能想使用re-Suggest也发布一些你尝试过的代码示例,注意问题被标记为3.4,所以你需要在那里抛出一个额外的列表来获得输出,例如listfilterlambda s:anyc.isalnum for c in s,l.请注意,问题标记为3.4,因此您需要在其中添加一个额外的列表以获得该输出,例如listfilterlambda s:anyc.isalnum for c in s,l。