我有一个文件:test.txt
,每行都有一个句子。
Hello World
99 Bottles of Beer
Humpty Dumpty Sat on the wall
我希望生成一个输出,显示此文件中输入的所有组合(即 2 个n-1 个组合)。在上面的例子中,算法将溢出以下内容 - 每个组合都用管道(|
) 分隔
Hello World
99 Bottles of Beer
Humpty Dumpty Sat on the wall
Hello World | 99 Bottles of Beer
Hello World | Humpty Dumpty Sat on the wall
99 Bottles of Beer | Humpty Dumpty Sat on the wall
Hello World | 99 Bottles of Beer | Humpty Dumpty Sat on the wall
理想情况下,我希望在 bash 或 python 或 perl 脚本中完成此操作,但我愿意接受建议。
import itertools
l = [s.strip() for s in open('test.txt')]
for i in range(len(l)):
print 'n'.join(map(' | '.join, itertools.combinations(l, i + 1)))
生产
Hello World
99 Bottles of Beer
Humpty Dumpty Sat on the wall
Hello World | 99 Bottles of Beer
Hello World | Humpty Dumpty Sat on the wall
99 Bottles of Beer | Humpty Dumpty Sat on the wall
Hello World | 99 Bottles of Beer | Humpty Dumpty Sat on the wall
如果你不喜欢'n'.join()
的风格(我不确定我是否喜欢),你可以用一个显式循环代替它:
for i in range(len(l)):
for c in map(' | '.join, itertools.combinations(l, i + 1)):
print c
这稍微啰嗦一些,但更经济。
你可以做
import itertools
file = open("test.txt")
lines = files.readlines()
current = []
for i in range(len(lines):
current.append(i)
for combination in set(itertools.permutations(current)):
for l in combination:
output+=' | '.join(lines[l])
output+= 'n'
print output
我对我的迭代工具和设置技能感到厌倦,但这应该有效,除非有内存限制。