适当的程序输出

时间:2011-05-16 13:03:01

标签: python

import itertools 

file = open('out.txt', 'w')
variations = itertools.product('abc', repeat=3) 

for variations in variations: 
    variation_string = "" 

    for letter in variations: 
        variation_string += letter 

    file.write(variation_string)

file.close()

上述程序的输出类似于聚集状态:

aaaaabaacabaabbabcacaacbaccbaababbacbbabbbbbcbcabcbbcccaacabcaccbacbbcbcccaccb­ccc

你可以修改程序,使得输出将在一行之后排成一行,输出的第一行是aaa,下一行是aab,下一行是aac aaa aab aac 等等......

{{1}}

3 个答案:

答案 0 :(得分:1)

file.write之后,添加另一个file.write('\n')

答案 1 :(得分:0)

答案 2 :(得分:0)

以下是如何将该列表打印到stdout:

>>> for variation in itertools.product('abc', repeat=3):
...     print(''.join(variation) + '\n'),

将其写入您的文件留作练习......