在Python中查找所有可能的大小写排列 [英] Finding all possible case permutations in Python
本文介绍了在Python中查找所有可能的大小写排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要计算python中仅大小写的所有可能排列的列表
例如,使用ar的输入,它将返回
['ar','Ar','aR','AR']
或弧
['arc','Arc','ARc','aRc','aRC','ARC'],我知道可能有一些不错的方法,但对于我的一生,我无法弄清楚.
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.
推荐答案
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']
这篇关于在Python中查找所有可能的大小写排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文