See*_*roy 3 python python-itertools
我有一个包含整数的列表,这些整数指示列表中一次会出现多少个大写字母。
x = [1, 2]
# when x == 1 then 1 capitalization per time
# when x == 2 then 2 capitalization per time
l = ['a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)
输出会像这样......
Abc
aBc
abC
ABc
AbC
aBC
Run Code Online (Sandbox Code Playgroud)
我可以正常编码,但是可以通过 itertools 完成吗?
使用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) ]))
Run Code Online (Sandbox Code Playgroud)
输出
Abc
aBc
abC
ABc
AbC
aBC
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
92 次 |
最近记录: |