Pergunta

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.

Foi útil?

Solução

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']
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top