-4

関数内で設定された長さの文字のすべての可能な組み合わせを再帰的に見つける python 関数を作成する必要があります。たとえば、単語 'unicorn' でこれを使用して、長さ 3 のすべての組み合わせを検索すると、次のように返されます。

['orN', 'crN', 'coN', 'cor', 'irN', 'ioN', 'ior', 'icN', 'icr', 'ico', 'nrN', 'noN', 'ノル', 'ncN', 'ncr', 'nco', 'niN', 'nir', 'nio', 'nic', 'urN', 'uoN', 'uor', 'ucN', 'ucr' , 'uco', 'uiN', 'uir', 'uio', 'uic', 'unN', 'unr', 'uno', 'unc', 'uni']

私はこれをどこから始めるべきかさえ知りません。私を助けてください。

4

1 に答える 1

2

itertools.combinations:

print [''.join(x) for x in itertools.combinations('unicorn', 3)]

要求したものとまったく同じ出力が必要な場合:

>>> print [''.join(x) for x in itertools.combinations('unicorN', 3)][::-1]
['orN', 'crN', 'coN', 'cor', 'irN', 'ioN', 'ior', 'icN', 'icr', 'ico', 'nrN', 'noN', 'nor', 'ncN', 'ncr', 'nco', 'niN', 'nir', 'nio', 'nic', 'urN', 'uoN', 'uor', 'ucN', 'ucr', 'uco', 'uiN', 'uir', 'uio', 'uic', 'unN', 'unr', 'uno', 'unc', 'uni']
于 2012-10-14T19:14:57.930 に答える