可以使用嵌套循环来生成所有的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/iXJx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录