加入元组的更有效方法?

坦克

有没有更有效的加入元组的方法?因为我的RX给我一个元组?而且c最后是'7:30''AM',我需要'7:30 AM'

import re

rx = r"(?i)\b(\d{1,2}:\d{2})(?:-\d{1,2}:\d{2})?(\s*[pa]m)\b"
s = "ankkjdf 7:30-8:30 AM dds "

matches = re.findall(rx, s)

m=str(matches)

a =''.join(m[2:8])
b= ''.join(m[9:15])

c = "".join(a + b)

print(c)
科波菲尔
>>> import re
>>> rx = r"(?i)\b(\d{1,2}:\d{2})(?:-\d{1,2}:\d{2})?(\s*[pa]m)\b"
>>> s = "ankkjdf 7:30-8:30 AM dds "
>>> matches = re.findall(rx, s)
>>> matches
[('7:30', ' AM')]
>>> [ "".join(x) for x in matches]
['7:30 AM']
>>> 

要么

>>> "".join(matches[0])
'7:30 AM'
>>> 

或直接从来源

>>> [ "".join(x) for x in re.findall(rx, s)]
['7:30 AM']
>>> "".join( re.findall(rx, s)[0] )
'7:30 AM'
>>>

没有理由去做m=str(matches),只要以您想要的任何方式融合您所得到的...


与最新的例子

>>> test="Join us for a guided tour of the Campus given by Admissions staff. The tour will take place from 1:15-2:00 PM EST and leaves from the Admissions Office."
>>> [ "".join(x) for x in re.findall(rx, test)]
['1:15 PM']
>>> "".join( re.findall(rx, test)[0] )
'1:15 PM'
>>> 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章