4

我有一个文件: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 脚本中完成此操作,但我愿意接受建议。

4

2 回答 2

3
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

这稍微有点冗长,但更经济。

于 2012-12-06T08:22:04.467 回答
0

你可以做

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

我厌倦了我的迭代工具和设置技能,但这应该可以工作,除非有内存限制..

于 2012-12-06T08:21:10.957 回答