我该如何将下面的双字母词组合并成一个字符串?
_bigrams=['the school', 'school boy', 'boy is', 'is reading']
_split=(' '.join(_bigrams)).split()
_newstr=[]
_filter=[_newstr.append(x) for x in _split if x not in _newstr]
_newstr=' '.join(_newstr)
print _newstr
输出结果:'the school boy is reading'
......虽然这是期望的输出结果,但由于数据量很大,所以这种方法过于冗长且效率不佳。其次,这种方法不能支持最终字符串中的重复单词,即'the school boy is reading, is he?'
。在这种情况下,只允许一个 'is'
出现在最终字符串中。
有什么好的建议可以让这个工作变得更好吗?谢谢。
no_runs_of_words()
函数更容易阅读。阅读算法的人不必关心该函数的实现方式,因为他们可以从名称清楚地知道它的作用。 - Amber_bigrams
条目为 (例如)"that that"
,则此方法将无法正常工作。 - DSM