Python3.4.3如何使textwrap与itertools一起工作

2024-04-18 12:25:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的代码是:

#!/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))    

herehere获取一些信息,但无法解决我的问题。你知道吗

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


Tags: 代码importenvfalsebinhereusrpython3
1条回答
网友
1楼 · 发布于 2024-04-18 12:25:34

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

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

但无论如何,这样做没有多大意义,因为序列的排列总是包含该序列的所有元素,所以组合的字符串总是具有相同的长度,只有内部单词的顺序会改变。你知道吗

所以你可以先做检查,然后再循环排列。你知道吗

或者可以生成允许不同长度的组合。你知道吗

相关问题 更多 >