【发布时间】:2020-07-25 16:32:15
【问题描述】:
我有一个嵌套的 list_3,它看起来像:
[['Company OverviewCompany: HowSector: SoftwareYear Founded: 2010One Sentence Pitch: Easily give and request low-quality feedback with your team to achieve more togetherUniversity Affiliation(s): Duke$ Raised: $240,000Investors: Friends & familyTraction to Date: 10% of monthly active users (MAU) are also active weekly'], [['Company OverviewCompany: GrubSector: SoftwareYear Founded: 2018One Sentence Pitch: Find food you likeUniversity Affiliation(s): Stanford$ Raised: $340,000Investors: Friends & familyTraction to Date: 40% of monthly active users (MAU) are also active weekly']]]
我想使用正则表达式在每个连接的单词之间添加一个逗号,后跟一个空格,即(HowSector:, SoftwareYear, 2010One),到目前为止,我已经尝试编写一个 re.sub 代码来做,通过选择所有没有空格的字符并替换它,但遇到了一些问题:
for i, list in enumerate(list_3):
list_3[i] = [re.sub('r\s\s+', ', ', word) for word in list]
list_33.append(list_3[i])
print(list_33)
错误:
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object
我希望输出是:
[['Company Overview, Company: How, Sector: Software, Year Founded: 2010, One Sentence Pitch: Easily give and request low-quality feedback with your team to achieve more together University, Affiliation(s): Duke, $ Raised: $240,000, Investors: Friends & family, Traction to Date: 10% of monthly active users (MAU) are also active weekly'],[...]]
任何想法我可以如何使用正则表达式来做到这一点?
【问题讨论】:
-
您打算如何区分
OverviewCompany和software或feedback?如果答案是“大写”,那么您尝试的正则表达式将无法正常工作 -
@DeepSpace 是的,我想尝试正则表达式来搜索以大写字母开头且它们之间没有任何空格的实例,这是正在查看的示例,geeksforgeeks.org/…,但我不能想办法。
-
stackoverflow.com/questions/15343163/…,这是一个类似的问题,但在 Java 中
-
', '.join(re.split(r'(?<=[a-z])(?=[A-Z])',each_string_in_nested_list))
标签: python regex nested-lists substitution word-spacing