给定如下文本列表:
text = ["Nanjing Office and Retail Market Overview 2019 Second Quarter",
"Xi'an Office and Retail Market Overview 2020 Q1",
"Suzhou office and retail overview 2019 fourth quarter DTZ Research",
"marketbeat Shanghai office Second quarter of 2020 Future New Grade A office supply in non-core business districts One-year trend Although the epidemic in Shanghai has been controlled in a timely and effective manner, the negative impact of the epidemic on Shanghai's commercial real estate The impact continues.",
"Shanghai office September 2019 marketbeats 302.7 -0.4% 12.9% rent rent growth vacancy"]
我想使用多个单词(Market, quarter, marketbeats
)来分割每个字符串元素,然后获得第一部分,包括分隔符:
for string in text:
# string = string.lower()
split_str = re.split(r"[Market|quarter|marketbeats]", string)
print(split_str)
:
['n', 'njing offic', ' ', 'nd ', '', '', '', 'il ', '', '', '', '', '', ' ov', '', 'vi', 'w 2019 ', '', 'cond ', '', '', '', '', '', '', '']
["xi'", 'n offic', ' ', 'nd ', '', '', '', 'il ', '', '', '', '', '', ' ov', '', 'vi', 'w 2020 ', '1'],
...
但是预期的结果将是这样的:
"Nanjing Office and Retail Market",
"Xi'an Office and Retail Market",
"Suzhou office and retail overview 2019 fourth quarter",
"marketbeat Shanghai office Second quarter",
"Shanghai office September 2019 marketbeats"
如何在Python中得到正确的结果?谢谢。
您可以在这里使用re.findall
方法:
text = ["Nanjing Office and Retail Market Overview 2019 Second Quarter", "Xi'an Office and Retail Market Overview 2020 Q1", "Suzhou office and retail overview 2019 fourth quarter DTZ Research", "marketbeat Shanghai office Second quarter of 2020 Future New Grade A office supply in non-core business districts One-year trend Although the epidemic in Shanghai has been controlled in a timely and effective manner, the negative impact of the epidemic on Shanghai's commercial real estate The impact continues.", "Shanghai office September 2019 marketbeats 302.7 -0.4% 12.9% rent rent growth vacancy"]
output = [re.findall(r'^.*?b(?:Market|quarter|marketbeats|$)b', x)[0] for x in text]
print(output)
这个打印:
['Nanjing Office and Retail Market',
"Xi'an Office and Retail Market",
'Suzhou office and retail overview 2019 fourth quarter',
'marketbeat Shanghai office Second quarter',
'Shanghai office September 2019 marketbeats']