一、combinations
KEYS = ['1','2','3']
combinations = list(itertools.combinations(KEYS, 2))
[('1', '2'), ('1', '3'), ('2', '3')]
二、product
KEYS = ['1','2','3']
combinations = list(itertools.product(KEYS, repeat=2))
print(combinations)
[('1', '1'), ('1', '2'), ('1', '3'), ('2', '1'), ('2', '2'), ('2', '3'), ('3', '1'), ('3', '2'), ('3', '3')]
三、combinations_with_replacement
KEYS = ['1','2','3']
combinations=listertools.combinations_with_replacement(KEYS,2))
print(combinations)
[('1', '1'), ('1', '2'), ('1', '3'), ('2', '2'), ('2', '3'), ('3', '3')]
三、itertools.permutations(KEYS,3)
KEYS = ['1','2','3']
combinations=listertools.permutations(KEYS,2))
print(combinations)
[('1', '2'), ('1', '3'), ('2', '1'), ('2', '3'), ('3', '1'), ('3', '2')]
移除相同元素的组合,例如 (‘A’, ‘A’)
combinations_without_same = [pair for pair in combinations if pair[0] != pair[1]]
将元组转换为字符串
result = [a + b for a, b in combinations_without_same]