all permutations of a binary sequence x bits long

前端 未结 5 1164
陌清茗
陌清茗 2020-11-30 03:57

I would like to find a clean and clever way (in python) to find all permutations of strings of 1s and 0s x chars long. Ideally this would be fast and not require doing too m

5条回答
  •  無奈伤痛
    2020-11-30 04:49

    itertools.product is made for this:

    >>> import itertools
    >>> ["".join(seq) for seq in itertools.product("01", repeat=2)]
    ['00', '01', '10', '11']
    >>> ["".join(seq) for seq in itertools.product("01", repeat=3)]
    ['000', '001', '010', '011', '100', '101', '110', '111']
    

提交回复
热议问题