如何将字符串拆分为 2 个单词的不同段,其中第二段的第一个单词重复第一个单词的最后一个单词

how to split a string into diferent segments of 2 words where the first word of the second segment repeats the last word of the first

我想弄清楚如何将字符串拆分为 2 个单词的片段,其中第二个片段的第一个单词重复第一个单词的最后一个单词。 (in Python 2)例如,"hi i am a human named joe norman"应该分成"hi i"、"i am"、"am joe"、"joe norman"。我有以下代码:

txt = raw_input("")

newtxt = txt.split(" ")

问题是它将 txt 按每个 space 拆分,而不是按每个其他拆分。我不想使用任何库。谢谢。

使用zip:

t = "hi i am a human named joe norman"
words = t.split()

result = list(zip(words, words[1:]))

for first, second in result:
    print("{} {}".format(first, second))

输出

hi i
i am
am a
a human
human named
named joe
joe norman

带 listcomp 的选项:

s = "hi i am a human named joe norman"
s = s.split()

l = [f'{i} {s[num + 1]}' for num, i in enumerate(s)
    if num + 1 < len(s)]

print(l) #['hi i', 'i am', 'am a', 'a human', 'human named', 'named joe', 'joe norman']

为了完整性,还有几个选项:

第一个版本大致基于 itertools.pairwise:

def pairs1(words):
    w2 = iter(words)
    next(w2, None)
    return zip(words, w2)

这很好,因为上面 Daniel 代码中的切片 (words[1:]) 创建了 words 列表的一个副本,它可能很大,而所需要的只是 [=32 处的迭代器=]位置

另一个版本使用 range():

def pairs2(words):
    for i in range(1, len(words)):
        yield (words[i-1], words[i])

这与 Mykola 的相似,但对我来说感觉更好。当然,两者都可以重写为使用列表理解,例如:

l = [f'{words[i-1]} {words[i]}' for i in range(1, len(words))]

相当于:

l = [f'{w} {x}' for w, x in pairs2(words)]