我有一个字符串列表,其中可能包含字母、符号、数字等,如下所示:
list = ['\n', '', '0', '38059', '', '', '?_', '71229366', '', '1', '38059', '', '', '?_', '87640804', '', '2', '38059', '', '', '?_', '71758011', '', '', ':?', ';__', '71229366287640804271758011287169822']
如何过滤除小于 10000000 和大于 99999999 的数字以外的所有其他字符串?
预期输出:
list = ['71229366', '87640804', '71758011']
最佳答案
您可以使用 map
和 filter
。
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/