2016-12-03 27 views
2

有没有更有效的方法来加入元组?既然我的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) 
+0

我猜你有其他的问题,如果你'S = “ankkjdf 11:30-1:30 PM DDS”',因为我怀疑' 11:30 PM是你想要的结果。 – pbuck

+0

这是我想要的结果。 – Tank

回答

2
>>> 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' 
>>> 
+0

它doens't工作我得到这个输出[[“1:15”,“PM”]] – Tank

+0

与什么输入? – Copperfield

+0

“加入我们,获得招生办公室工作人员指导,参观时间为美国东部时间下午1:15-2:00,并从招生办公室离开。”我正在抓取数据以便发生某些事情? – Tank

相关问题