python - 从字符串列表中提取 8 位数字

标签 python list text-extraction

我有一个字符串列表,其中可能包含字母、符号、数字等,如下所示:

list = ['\n', '', '0', '38059', '', '', '?_', '71229366', '', '1', '38059', '', '', '?_', '87640804', '', '2', '38059', '', '', '?_', '71758011', '', '', ':?', ';__', '71229366287640804271758011287169822']

如何过滤除小于 10000000 和大于 99999999 的数字以外的所有其他字符串?

预期输出:

list = ['71229366', '87640804', '71758011']

最佳答案

您可以使用 mapfilter

your_list = ['\n', '', '0', '38059', '', '', '?_', '71229366', '', '1', '38059', 
             '', '', '?_', '87640804', '', '2', '38059', '', '', '?_', '71758011', 
             '', '', ':?', ';__', '71229366287640804271758011287169822']

new_list = list(map(int, filter(lambda x: x.isdigit() and 10000000 < int(x) < 99999999, your_list)))
print(new_list)

list() 在 python2 上可选。

输出:

[71229366, 87640804, 71758011]

如果您不想转换为整数,请删除 map:

>>> list(filter(lambda x: x.isdigit() and 10000000 < int(x) < 99999999, your_list))
['71229366', '87640804', '71758011']

关于python - 从字符串列表中提取 8 位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44958629/

相关文章:

python - Python 字典中 switch/case 替换失败

python - 如何将列表复制一定次数

python - 如何从链接中提取经度和纬度

c++ - 提取符号之间的字符串

PDF表格文本提取

python - 如何将字符串列表更改为整数 - Python

python - 将函数的输出分配给python中的空列表常量

python - ArcPy 和 Python 编码搞砸了?

Python 列表 - True/False 前两个元素?

python - 列表理解合并python中的各种列表