Find all upper, lower and mixed case combinations of a string

后端 未结 6 1326
名媛妹妹
名媛妹妹 2020-11-29 04:08

I want to write a program that would take a string, let\'s say \"Fox\", then it would display:

fox, F         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 04:52

    >>> import itertools
    >>> map(''.join, itertools.product(*((c.upper(), c.lower()) for c in 'Fox')))
    ['FOX', 'FOx', 'FoX', 'Fox', 'fOX', 'fOx', 'foX', 'fox']
    

    Or

    >>> s = 'Fox'
    >>> map(''.join, itertools.product(*zip(s.upper(), s.lower())))
    

提交回复
热议问题