在python中使用正则表达式删除括号中的内容
我有一个在python中使用正则表达式删除括号中的内容,python,regex,list,python-2.7,Python,Regex,List,Python 2.7,我有一个列表: ['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL'] 也作为字符串'14147618(100%)6137776(43%)5943229(42%)2066613(14%)总计\n' 使用regex,如何返回: ['14147618', '6137776, '5943229', 2066613'] 你根本不需要正则表达式,你可以用这个列表简单地过滤掉只
列表
:
['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
也作为字符串'14147618(100%)6137776(43%)5943229(42%)2066613(14%)总计\n'
使用regex,如何返回:
['14147618', '6137776, '5943229', 2066613']
你根本不需要正则表达式,你可以用这个列表简单地过滤掉只有数字的数据
print [item for item in data if item.isdigit()]
# ['14147618', '6137776', '5943229', '2066613']
或者也可以使用内置函数,如下所示
print filter(str.isdigit, data)
# ['14147618', '6137776', '5943229', '2066613']
编辑:如果将整个数据作为单个字符串,则可以基于空格字符拆分数据,然后使用相同的逻辑
data = '14147618 (100%) 6137776 (43%) 5943229 (42%) 2066613 (14%) TOTAL\n'
print [item for item in data.split() if item.isdigit()]
# ['14147618', '6137776', '5943229', '2066613']
print filter(str.isdigit, data.split())
# ['14147618', '6137776', '5943229', '2066613']
你根本不需要正则表达式,你可以用这个列表简单地过滤掉只有数字的数据
print [item for item in data if item.isdigit()]
# ['14147618', '6137776', '5943229', '2066613']
或者也可以使用内置函数,如下所示
print filter(str.isdigit, data)
# ['14147618', '6137776', '5943229', '2066613']
编辑:如果将整个数据作为单个字符串,则可以基于空格字符拆分数据,然后使用相同的逻辑
data = '14147618 (100%) 6137776 (43%) 5943229 (42%) 2066613 (14%) TOTAL\n'
print [item for item in data.split() if item.isdigit()]
# ['14147618', '6137776', '5943229', '2066613']
print filter(str.isdigit, data.split())
# ['14147618', '6137776', '5943229', '2066613']
正如@thefourtheye所说,根本没有必要使用regex,但是如果您真的想使用regex,您可以使用:
import re
a = ['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
result = []
for e in a:
m = re.match(r'\d+', e)
if m is not None:
result.append(e)
print result
# ['14147618', '6137776', '5943229', '2066613']
注意:这也可以写成列表理解:
print [e for e in a if re.match(r'\d+', e)]
正如@thefourtheye所说,根本没有必要使用regex,但是如果您真的想使用regex,您可以使用:
import re
a = ['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
result = []
for e in a:
m = re.match(r'\d+', e)
if m is not None:
result.append(e)
print result
# ['14147618', '6137776', '5943229', '2066613']
注意:这也可以写成列表理解:
print [e for e in a if re.match(r'\d+', e)]
这里有一个方法:
>>> l = ['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
>>> [el for el in l if re.match(r'\d+$', el)]
['14147618', '6137776', '5943229', '2066613']
这里有一个方法:
>>> l = ['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
>>> [el for el in l if re.match(r'\d+$', el)]
['14147618', '6137776', '5943229', '2066613']
使用re模块:
>>> import re
>>> [item for item in s if re.match(r'\d+',item)]
['14147618', '6137776', '5943229', '2066613']
使用re模块:
>>> import re
>>> [item for item in s if re.match(r'\d+',item)]
['14147618', '6137776', '5943229', '2066613']
根本不需要使用
re
模块,您可以在列表
上使用过滤器
试试这个
>>> a=['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
>>> filter(str.isdigit, a)
['14147618', '6137776', '5943229', '2066613']
>>>
根本不需要使用
re
模块,您可以在列表
上使用过滤器
试试这个
>>> a=['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
>>> filter(str.isdigit, a)
['14147618', '6137776', '5943229', '2066613']
>>>
或者,如果您想要除最后一个之外的偶数索引元素:
print [data[i] for i in range(0,len(data)-1,2)]
或者,如果您想要除最后一个之外的偶数索引元素:
print [data[i] for i in range(0,len(data)-1,2)]
你能告诉我a=
'14147618(100%)6137776(43%)5943229(42%)2066613(14%)总数是多少吗?
我怎么才能得到['14147618','6137776','5943229',2066613'
?@VeilEclipse你可以使用与a.split()相同的程序。
你能告诉我a='14147618(100%)6137776',6137776',59439',20663(42%)吗总计\n'
如何获取['14147618','6137776','5943229',2066613']
?@VeilEclipse您可以使用与a.split()相同的程序。