47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
from itertools import combinations
|
|
|
|
|
|
def generate_combinations(n, suffix):
|
|
# 所有可能的单个字母
|
|
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
# 对 n=1 的特殊处理
|
|
if n == 1:
|
|
return [c + suffix for c in letters]
|
|
|
|
# 1. n 个字母完全相同的组合
|
|
repeated_combinations = [c * n for c in letters]
|
|
|
|
# 2. (n-1) 个字母相同,一个不同的组合 (仅当 n > 1)
|
|
double_repeats = []
|
|
if n > 1:
|
|
for c1 in letters:
|
|
for c2 in letters:
|
|
if c1 != c2:
|
|
# 确保字母按字母表顺序排列,避免重复
|
|
combination = ''.join(sorted([c1] * (n - 1) + [c2]))
|
|
if combination not in double_repeats:
|
|
double_repeats.append(combination)
|
|
|
|
# 3. n 个字母不完全相同的组合 (仅当 n > 1)
|
|
unique_combinations = []
|
|
if n > 1:
|
|
unique_combinations = [''.join(sorted(comb)) for comb in combinations(letters, n) if len(set(comb)) == n]
|
|
|
|
# 合并所有组合
|
|
all_combinations = repeated_combinations + double_repeats + unique_combinations
|
|
|
|
# 将所有组合拼接后缀
|
|
final_combinations = [combo + suffix for combo in all_combinations]
|
|
|
|
return final_combinations
|
|
|
|
if __name__ == '__main__':
|
|
n = 2 # 可以输入不同的长度
|
|
suffix = '.de' # 可以输入不同的后缀
|
|
|
|
# 生成组合并打印
|
|
combinations = generate_combinations(n, suffix)
|
|
print(combinations)
|
|
print(f"生成的组合数量: {len(combinations)}")
|