我有一个包含整数的列表,它指示一个列表中将同时出现多少大写。
x = [1, 2]
# when x == 1 then 1 capitalization per time
# when x == 2 then 2 capitalization per time
l = ['a', 'b', 'c']输出结果会是这样..。
Abc
aBc
abC
ABc
AbC
aBC我可以正常地编写这段代码,但是可以通过迭代工具来完成吗?
发布于 2021-10-10 09:02:56
使用itertools.combinations选择字母的索引以大写:
from itertools import combinations
x = [1, 2]
l = ['a', 'b', 'c']
for xi in x:
for comb in combinations(range(len(l)), xi):
print("".join([e.upper() if i in comb else e for i, e in enumerate(l) ]))输出
Abc
aBc
abC
ABc
AbC
aBChttps://stackoverflow.com/questions/69513745
复制相似问题