[英]Extract 8-digit numbers from a list of strings
I have a list of strings which may contain letters, symbols, digits, etc, as below: 我有一个字符串列表,其中可能包含字母,符号,数字等,如下所示:
list = ['\n', '', '0', '38059', '', '', '?_', '71229366', '', '1', '38059', '', '', '?_', '87640804', '', '2', '38059', '', '', '?_', '71758011', '', '', ':?', ';__', '71229366287640804271758011287169822']
How do I filter out all other strings, except numbers less than 10000000 and greater than 99999999? 如何过滤掉所有其他字符串,数字小于10000000且大于99999999的字符串除外?
Expected Output: 预期产量:
list = ['71229366', '87640804', '71758011']
如果您不介意制作新列表,则可以尝试使用列表理解功能,例如
filtered_list = [i for i in list if i.isdigit() and 10000000 < int(i) < 99999999]
You can use a map
and filter
. 您可以使用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()
optional on python2. list()
在python2上是可选的。
Output: 输出:
[71229366, 87640804, 71758011]
If you don't want the conversion to integer, drop the map
: 如果您不希望转换为整数,请删除map
:
>>> list(filter(lambda x: x.isdigit() and 10000000 < int(x) < 99999999, your_list))
['71229366', '87640804', '71758011']
def valid(v):
try:
value = int(v)
return 10000000 <= value <= 99999999
except:
return False
output = [x for x in list if valid(x)]
Explanation : 说明 :
Filter all values in the list using the valid function as your criteria. 使用有效函数作为条件过滤列表中的所有值。
data = ['\n', '', '0', '38059', '', '', '?_', '71229366', '', '1', '38059',
'', '', '?_', '87640804', '', '2', '38059', '', '', '?_', '71758011',
'', '', ':?', ';__', '71229366287640804271758011287169822']
res = []
for e in data:
try:
number = int(e)
except ValueError:
continue
if 10000000 < number < 99999999:
res.append(str(number))
print(res) 打印(res)
print(res)
Output: 输出:
['71229366', '87640804', '71758011']
Let me provide a simple and efficient answer, using regular expressions. 让我使用正则表达式提供一个简单而有效的答案。 There's no need to map
(duplicating the original list), or to convert everything to int
s; 无需map
(复制原始列表)或将所有内容都转换为int
; you are basically asking how to keep all 8-digit integers in your list: 您基本上是在问如何将所有8位整数保留在列表中:
>>> filter(re.compile('^\d{8}$').match, data)
['71229366', '87640804', '71758011']
We compile
a regular expression which matches exactly 8 digits and then filter the list by providing a partial application of regex.match
to the standard filter
function. 我们compile
一个正则表达式,精确匹配8位数字,然后通过向标准filter
函数提供regex.match
的部分应用来过滤列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.