Python 脚本生成所有 3 位数字组合 (0, 1, 2)
使用 Python 语言写一个脚本生成由 0, 1, 2 组成的所有 3 位数组合,数字可以重复。可以使用嵌套循环来生成所有的 3 位数组合。以下是一个示例脚本:
def generate_combinations():
combinations = []
digits = [0, 1, 2]
for i in digits:
for j in digits:
for k in digits:
combination = [i, j, k]
combinations.append(combination)
return combinations
all_combinations = generate_combinations()
for combination in all_combinations:
print(combination)
输出结果将是:
[0, 0, 0]
[0, 0, 1]
[0, 0, 2]
[0, 1, 0]
[0, 1, 1]
[0, 1, 2]
[0, 2, 0]
[0, 2, 1]
[0, 2, 2]
[1, 0, 0]
[1, 0, 1]
[1, 0, 2]
[1, 1, 0]
[1, 1, 1]
[1, 1, 2]
[1, 2, 0]
[1, 2, 1]
[1, 2, 2]
[2, 0, 0]
[2, 0, 1]
[2, 0, 2]
[2, 1, 0]
[2, 1, 1]
[2, 1, 2]
[2, 2, 0]
[2, 2, 1]
[2, 2, 2]
这样就生成了所有由 0、1、2 组成的 3 位数组合。
原文地址: https://www.cveoy.top/t/topic/qDJp 著作权归作者所有。请勿转载和采集!