我已经创建了这个“two for loops”方法,在这个方法中,假设我将一个句子标记为一个列表[w1,w2,w3,…,wn],我想输出以下几对(不是bigrams)
for i in range(len(words)):
for j in range(i+1, range(len(words))):
couples = (words[i], words[j])
但是我想要lambda函数格式,以便在我的apachespark程序中使用。有人能帮我吗?你知道吗
提前谢谢
我将从直接遍历列表开始:
然而,这给出了完整的笛卡尔积,它与你原来得到的不完全相同。所以我们需要第一个循环的索引,而不是第二个循环的索引:
现在我们可以将其转换为生成器表达式(而不是lambda):
使用itertools的组合将得到您想要的结果。你知道吗
相关问题 更多 >
编程相关推荐