Ore*_*ija 6 python string tuples list python-3.x
问题如下.我有一个字符串列表
lst1=['puffing','his','first','cigarette','in', 'weeks', 'in', 'weeks']
Run Code Online (Sandbox Code Playgroud)
我想获得这个字符串
lst2=['puffing','his','first','cigarette','in weeks', 'in weeks']
Run Code Online (Sandbox Code Playgroud)
这是为了连接子列表的任何出现['in', 'weeks'],原因与此处无关,find_sub_list1从这里取得(并包含在下面的代码中):
npis = [['in', 'weeks'], ['in', 'ages']]
# given a list a candidate sublist, return the index of the first and last
# element of the sublist within the list
def find_sub_list1(sl,l):
results=[]
sll=len(sl)
for ind in (i for i,e in enumerate(l) if e==sl[0]):
if l[ind:ind+sll]==sl:
results.append((ind,ind+sll-1))
return results
def concatenator(sent, npis):
indices = []
for npi in npis:
indices_temp = find_sub_list1(npi, sent)
if indices_temp != []:
indices.extend(indices_temp)
sorted(indices, key=lambda x: x[0])
for (a,b) in indices:
diff = b - a
sent[a:b+1] = [" ".join(sent[a:b+1])]
del indices[0]
indices = [(a - diff, b - diff) for (a,b) in indices]
return sent
Run Code Online (Sandbox Code Playgroud)
而不是所需的lst2编码器返回:
concatenator(lst1,['in', 'weeks'])
>>['puffing','his','first','cigarette','in weeks', 'in', 'weeks']
Run Code Online (Sandbox Code Playgroud)
所以它只连接第一次出现.关于代码失败的地方的任何想法?
这不是对您的代码的修复,而是替代解决方案(我总是最终使用正则表达式来处理所有事情)
import re
list1_str = ','.join(lst1)
npis_concat = [','.join(x) for x in npis]
for item in npis_concat:
list1_str = re.sub(r'\b'+item+r'\b',item.replace(',', ' '),list1_str)
lst1 = list1_str.split(',')
Run Code Online (Sandbox Code Playgroud)
我在这里使用逗号,但您可以将其替换为任何字符,最好是您知道文本中不会出现的字符
它们r'\b'用于确保我们不会意外地从以 npi 中的内容结尾/开头的单词中截取位
| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |