list_1=["TP","MP","TS"]
list_2=["RTS:Id The package is delivered to TEMPR13TS0002",
"RTS:Id The package is delivered to TEMPS19TS0332"]
我试图在列表2元素的子字符串中查找列表1的元素,并按如下方式替换它们:
对于TS, 输出应为
list_2=["RTS:Id The package is delivered to TEMPR13 TS",
"RTS:Id The package is delivered to TEMPS19 TS"]
在TS的左边插入空格并删除右边的任何内容。你知道吗
相反,我得到的输出是:
list_2=["R TS:Id The package is delivered to TEMPR13 TS",
"R TS:Id The package is delivered to TEMPS19 TS"]
我面临的问题,因为它也会做同样的事情,为RTS子串。我只想执行操作的子串长度大于10。你知道吗
我的列表+正则表达式如下:
updated_list=[ re.sub(r'(' + '|'.join(list_1) + ')\S+',
r' \1', i)for i in list_2]
正则表达式不适合检查字符串长度。你知道吗
您可以改变
list_1
来处理RTS:Id
和13TS00
之间的差异,或者使用其他Python函数来搜索、检查和替换字符串。你知道吗仅当TS后面跟一个数字时匹配
list_1=["TP","MP","TS\d"]
不是很有效的解决方案:
相关问题 更多 >
编程相关推荐