Python 3.4.3 - 如何使 textwrap 与 itertools 一起工作

Python 3.4.3 - How to make textwrap work with itertools

我正在使用此代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import textwrap
import itertools

a = []

a.append('word1')
a.append('word2')
a.append('word3')
a.append('word4')
a.append('word5')
a.append('word6')
a.append('word7')
a.append('word8')
a.append('word9')
a.append('word10')

for permutation in itertools.permutations(a,):
    permutation = textwrap.TextWrapper(len=60,break_long_words=False,replace_whitespace=False)
    print(' '.join(permutation))    

并从here and here获取了一些信息,但无法解决我的问题。

我只想将这些单词排列成所有可能的组合,不可重复的短语长度为 60 个字符。

这将打印所有排列组合成一个字符串时,长度为 60:

for permutation in itertools.permutations(a):
    s = ' '.join(permutation)
    if len(s) == 60:
        print(s)

但是无论如何这样做都没有多大意义,因为序列的排列将始终包含该序列的所有元素,因此组合字符串将始终具有相同的长度,并且只有单词的顺序内部变化。

因此您也可以先进行检查,然后再遍历排列。

或者您可以生成允许不同长度的组合。