Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
相邻对的Python映射器_Python_Mapreduce_Stdin - Fatal编程技术网

相邻对的Python映射器

相邻对的Python映射器,python,mapreduce,stdin,Python,Mapreduce,Stdin,我正在尝试编写一个mapreduce程序,这是map部分,它从标准文本返回bigram或相邻单词对 这是我的概念/半伪: for line in sys.stdin: line = line.strip() words = line.split() for pair in words: #HERE*** print '%s\t%s' % (pair,1) 我如何提取一对相邻的单词,以便输出所有相邻的单词对,例如“word1 word2,1”,以便在我的

我正在尝试编写一个mapreduce程序,这是map部分,它从标准文本返回bigram或相邻单词对

这是我的概念/半伪:

for line in sys.stdin:
    line = line.strip()
    words = line.split()

    for pair in words: #HERE***
        print '%s\t%s' % (pair,1)
我如何提取一对相邻的单词,以便输出所有相邻的单词对,例如“word1 word2,1”,以便在我的reducer中组合它们?我想保持格式尽可能接近这个


多谢各位

您可以这样配对:

from itertools import tee

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

非常感谢。现在,如果一行只有一个单词,那么它将输出一个空白对[],我如何摆脱这个/只有当它们在那里时才显示它?只显示一个if语句怎么样
如果有多个单词
,则将它们配对,否则,只需打印出该单词/执行其他操作即可。