问题
I need to work out a list of all possible permutations of case only in python
for example with input of ar it would return
[ 'ar','Ar','aR','AR']
or arc
[ 'arc','Arc','ARc','aRc','aRC','ARC'] and I know there is likely some Nice method but for the life of me I can not figure it out.
回答1:
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']
来源:https://stackoverflow.com/questions/6792803/finding-all-possible-case-permutations-in-python