开发者

Finding all possible case permutations in Python [duplicate]

开发者 https://www.devze.com 2023-03-22 02:19 出处:网络
This question already has answers here: 开发者_运维问答Find all upper, lower and mixed case combinations of a string
This question already has answers here: 开发者_运维问答 Find all upper, lower and mixed case combinations of a string (7 answers) Closed 1 year ago.

I need to return a list of all possible case permutations of a string in python.

For example, input "ar" should return:

[ 'ar','Ar','aR','AR']  

or "arc":

[ 'arc','Arc','ARc','aRc','aRC','ARC']


def all_casings(input_string):
    if not input_string:
        yield ""
    else:
        first = input_string[:1]
        if first.lower() == first.upper():
            for sub_casing in all_casings(input_string[1:]):
                yield first + sub_casing
        else:
            for sub_casing in all_casings(input_string[1:]):
                yield first.lower() + sub_casing
                yield first.upper() + sub_casing

>>> [x for x in all_casings("foo")]
['foo', 'Foo', 'fOo', 'FOo', 'foO', 'FoO', 'fOO', 'FOO']
>>> list(all_casings("foo"))
['foo', 'Foo', 'fOo', 'FOo', 'foO', 'FoO', 'fOO', 'FOO']


You can achieve this by zipping the upper and lower case letters and taking their cartesian product:

import itertools

chars = "abc"
results = list(map(''.join, itertools.product(*zip(chars.upper(), chars.lower()))))

print(results)
>>>['ABC', 'ABc', 'AbC', 'Abc', 'aBC', 'aBc', 'abC', 'abc']

To visualise how this works:

  1. zip is creating our 3 'axes' for us, each with 2 points (the upper / lower cases):
    [('A', 'a'), 
     ('B', 'b'), 
     ('C', 'c')]
    
  2. product takes the cartesian product of these axes, i.e. the 8 possible coordinates corresponding to the corners of the unit cube it creates:

Finding all possible case permutations in Python [duplicate]

Finding all possible case permutations in Python [duplicate]

1. Create axes 2. Take Cartesian product
  1. ''.join concatenates the tuples to output a string: ('A','B','C') -> 'ABC'
0

精彩评论

暂无评论...
验证码 换一张
取 消