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

前端 未结 1 1257
一整个雨季
一整个雨季 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)
提交回复
热议问题