如何在Python中合并单个文本文件的行?

发布于 2024-12-11 06:25:26 字数 231 浏览 1 评论 0原文

我已经搜索过,但没有找到任何帮助。这是一个例子:

    List.txt
    a
    b
    c
    d

我希望能够得到这样的输出:

    Output.txt
    ab
    ac
    ad
    ba
    bc
    bd
    ca
    cb
    cd
    etc...

I've searched, but found nothing to help.. This is an example:

    List.txt
    a
    b
    c
    d

I want to be able to get an output like this:

    Output.txt
    ab
    ac
    ad
    ba
    bc
    bd
    ca
    cb
    cd
    etc...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦醒灬来后我 2024-12-18 06:25:27

itertools 模块具有组合函数来帮助解决此类问题:

>>> from itertools import combinations, permutations, product
>>> s = open('list.txt').read().splitlines()
>>> for t in permutations(s, 2):
        print ''.join(t)

The itertools module has combinatoric functions to help with problems like this one:

>>> from itertools import combinations, permutations, product
>>> s = open('list.txt').read().splitlines()
>>> for t in permutations(s, 2):
        print ''.join(t)
苏佲洛 2024-12-18 06:25:27

您可以首先将文件读入数组:

lines=[]
for line in file:
  lines.append(line)

然后迭代它以获得所需的输出。

for line1 in lines:
  for line2 in lines:
    print line1+line2

或将其打印到文件中。

You can first read the file into an array:

lines=[]
for line in file:
  lines.append(line)

and then iterate over it to get the desired output.

for line1 in lines:
  for line2 in lines:
    print line1+line2

or print it to a file instead.

与往事干杯 2024-12-18 06:25:26

非常简单...

from itertools import permutations

with open('List.txt') as f:    
    letters = (l.strip() for l in f if l.strip())    
    for p in permutations(letters, 2):
        print ''.join(p)

输出:

ab
ac
ad
ba
bc
bd
ca
cb
cd
da
db
dc

一些注释:

with 语句确保文件在使用完毕后将被关闭。

letters 是一个生成器表达式,在许多情况下(尽管不是这个)它可以让您不必一次读取整个文件。

使用 l.strip() 旨在很好地处理输入中出现的意外空行。

itertools.permutations 是正确的,而不是 itertools.combinations ,它认为 ab == ba 并且不包括后者作为输出。

快乐的蟒蛇:)

Pretty straightforward...

from itertools import permutations

with open('List.txt') as f:    
    letters = (l.strip() for l in f if l.strip())    
    for p in permutations(letters, 2):
        print ''.join(p)

Output:

ab
ac
ad
ba
bc
bd
ca
cb
cd
da
db
dc

Some notes:

The with statement ensures the file will be closed when you're done with it.

letters is a generator expression, which in many cases (though not this one) will save you from having to read the entire file in at once.

The uses of l.strip() are meant to nicely handle accidental blank lines if present in input.

itertools.permutations is correct, NOT itertools.combinations which considers ab == ba and will not include the latter as output.

Happy pythoning :)

久而酒知 2024-12-18 06:25:26
f = open("List.txt")
lines = f.read().splitlines()
lines_new = []
for line in lines:
    for line2 in lines:
        if not line == line2:
            lines_new.append("%s%s" % (line, line2))

print lines_new # ['ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc']
open("Output.txt", "w").write("\n".join(lines_new))

生成一个名为 Output.txt 的文件,其中包含:

ab
ac
ad
ba
bc
bd
ca
cb
cd
da
db
dc
f = open("List.txt")
lines = f.read().splitlines()
lines_new = []
for line in lines:
    for line2 in lines:
        if not line == line2:
            lines_new.append("%s%s" % (line, line2))

print lines_new # ['ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc']
open("Output.txt", "w").write("\n".join(lines_new))

Results in a file called Output.txt with:

ab
ac
ad
ba
bc
bd
ca
cb
cd
da
db
dc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文