문제

A really simple question:

word = 'toy'

I want to generate following:

word_altered_cases = ['toy', 'Toy', 'tOy', 'toY', 'TOy', 'tOY', 'ToY', 'TOY']

I went this far:

for char in word:
  word.replace(char, char.upper())

But obviously, it will produce incompelete permutation and will replace all chars present in word.

도움이 되었습니까?

해결책

Using itertools.product:

>>> import itertools
>>> word = 'toy'
>>> [''.join(w) for w in itertools.product(*zip(word.lower(), word.upper()))]
['toy', 'toY', 'tOy', 'tOY', 'Toy', 'ToY', 'TOy', 'TOY']
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top