Is there a function in Python which generates all the strings of length n over a given alphabet?

前端 未结 1 1248
一整个雨季
一整个雨季 2021-02-14 03:32

I need a function generateAllStrings(n, alphabet) to do something like this:

generateAllStrings(4, [\'a\',\'b\'])
>>> [\"aaaa\", \"aaab\",          


        
1条回答
  •  有刺的猬
    2021-02-14 04:13

    >>> [''.join(i) for i in itertools.product("ab",repeat=4)]
    ['aaaa', 'aaab', 'aaba', 'aabb', 'abaa', 'abab', 'abba', 'abbb', 'baaa', 'baab', 'baba', 'babb', 'bbaa', 'bbab', 'bbba', 'bbbb']
    

    0 讨论(0)
提交回复
热议问题