首页 > 解决方案 > 根据条件将列表的元素连接到一个新列表中

问题描述

我有一个带有句子的列表。和另一个带有几句话的列表。

sentences=['this is first', 'this is one', 'this is three','this is four','this is five','this is six']
exceptions=['one','four']

我想遍历句子,如果一个句子以 [exceptions] 中包含的单词之一结尾,则与下一个句子连接。

结果:

sentences2=['this is first', 'this is one this is three','this is four this is five','this is six']

我无法提出任何接近工作的合理尝试。

我从一个循环开始,然后将列表转换为迭代器:

myter = iter(sentences)

然后尝试连接句子并将连接的句子附加到句子2中。

都无济于事。

我的最后一次尝试是:

i=0
while True:
    try:
        if sentences[i].split(' ')[-1] in exceptions:
            newsentence = sentence[i] + sentence[i+1]
            sentences[i] = newsentence
            sentences.pop(i+1)
            i = i +1
        else:
            i=i+1
    except:
        break

 print('\n-----\n'.join(sentences))

不知何故,我觉得我正在用错误的方法尝试它。

谢谢。

标签: pythonstringlist

解决方案


您可以使用itertoolssentences使用自身的一个偏移切片进行压缩。zip_longest这将让您执行检查,在需要时进行连接,并在不需要时跳过下一次迭代。

from itertools import zip_longest

sentences2 = []
skip = False
for s1, s2 in zip_longest(sentences, sentences[1:]):
    if skip:
        skip = False
        continue
    if s1.split()[-1].lower() in exceptions:
        sentences2.append(f'{s1} {s2}')
        skip = True
    else:
        sentences2.append(s1)

sentences2
# returns:
['this is first', 'this is one this is three', 'this is four this is five', 'this is six']

编辑:

您需要处理连续连接多个句子的情况。对于这种情况,您可以使用标志来跟踪您是否应该加入下一个句子。它有点混乱,但这里是:

sentences2 = []
join_next = False
candidate = None
for s in sentences:
    if join_next:
        candidate += (' ' + s)
        join_next = False
    if candidate is None:
        candidate = s
    if s.split()[-1].lower() in exceptions:
        join_next = True
        continue
    else:
        sentences2.append(candidate)
        candidate = None

sentences2
# returns:
['this is first',
 'this is one this is three',
 'this is four this is five',
 'this is six']

这是一个添加需要链连接的额外句子的示例。

sentences3 = ['this is first', 'this is one', 'extra here four', 
              'this is three', 'this is four', 'this is five', 'this is six']

sentences4 = []
join_next = False
candidate = None
for s in sentences3:
    if join_next:
        candidate += (' ' + s)
        join_next = False
    if candidate is None:
        candidate = s
    if s.split()[-1].lower() in exceptions:
        join_next = True
        continue
    else:
        sentences4.append(candidate)
        candidate = None

sentences4
# returns:
['this is first',
 'this is one extra here four this is three',
 'this is four this is five',
 'this is six']

推荐阅读