python - 加入元组的更有效方法?

标签 python string

有没有更有效的方法来连接元组?因为我的 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'
>>> 

关于python - 加入元组的更有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40953247/

相关文章:

python - 等待特定延迟后如何在python中启动线程

python - pandas DataFrame 爆炸列内容

java - 使用 Java 从文本/字符串中获取特定 URL

c++ - 逗号分隔标记到 const char 的 vector **

c# - 如何验证 GUID 是一个 GUID

python - 接收 key 错误 : "None of [Int64Index([ ... dtype=' int6 4', length=1323)] are in the [columns]"

python - Scrapy:scrapy server需要一个项目,为什么?

string - 在两个大文件之间匹配字符串的算法

Python - 从串行端口数据逐行读取到可用的列表中

java - String.replace 不工作