本文介绍了字符串所有可能情况的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个程序来生成python中字符串的所有可能的大写形式.例如,给定"abcedfghij",我想要一个程序生成:AbcdefghijABcdef ....aBcdef ...ABCDEFGHIJ
I am trying to create a program to generate all possible capitalization cases of a string in python. For example, given 'abcedfghij', I want a program to generate:AbcdefghijABcdef....aBcdef...ABCDEFGHIJ
以此类推.我正在尝试找到一种快速的方法,但是我不知道从哪里开始.
And so on. I am trying to find a quick way to do it, but I don't know where to start.
推荐答案
from itertools import product, izip
def Cc(s):
s = s.lower()
for p in product(*[(0,1)]*len(s)):
yield ''.join( c.upper() if t else c for t,c in izip(p,s))
print list(Cc("Dan"))
打印:
['dan', 'daN', 'dAn', 'dAN', 'Dan', 'DaN', 'DAn', 'DAN']
这篇关于字符串所有可能情况的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!