【问题标题】:Iterate to get case alternatives in python迭代以在 python 中获取案例替代方案
【发布时间】:2014-02-10 13:41:54
【问题描述】:

一个非常简单的问题:

word = 'toy'

我想生成以下内容:

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

我走了这么远:

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

但显然,它会产生不完整的排列,并会替换 word 中存在的所有字符。

【问题讨论】:

标签: python list permutation case-sensitive


【解决方案1】:

使用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']

【讨论】:

    猜你喜欢
    • 2018-06-06
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多