temp1 = []
filtered_tokens = ['once','upon','a','time','in','the','forest']
for i in range(len(filtered_tokens)):
for j in range(i+1,len(filtered_tokens)):
temp1.append(filtered_tokens[i]+' '+filtered_tokens[j])
filetered_list,上面代码中包含10,000个单词,我只给出了示例列表。
我需要的输出:Once upon, once a, once in, once the, once forest , a the, a forest, time in, time the, time forest, 在, in forest, the forest
当我编写逻辑并运行它时,编译器在运行时向我抛出低内存异常。
请帮助我如何使用组合或任何其他 python 语言解决此问题。
谢谢
请您参考如下方法:
对于组合,您可以使用 itertools 模块。
import itertools
temp1 = itertools.combinations(filtered_tokens, 2)
将创建 2 个单词的所有组合。如果你想要列表,只需转换生成器:
temp1 = list(temp1)
