首页 > 解决方案 > 创建布尔列表的列表

问题描述

我需要得到以下信息:([[True, True, False, False, False, False],[ False, False,True, True, False, False],[False, False, False, False,True, True, ]])

我写了以下内容:

def create_bool():
    x_bool =[True, True, False, False,
                     False, False]
    arr_bool = []
    for i in range(3):
        arr_bool.append(x_bool)
        print(arr_bool)
        x_bool[:] = x_bool[-2:] + x_bool[0:-2]
        i+=1
    return arr_bool

但我得到了:([[True, True, False, False, False, False], [True, True, False, False, False, False], [True, True, False, False, False, False]])

标签: pythonloopsbooleansequenceboolean-operations

解决方案


您需要删除 x_bool set 语句的索引,并注释 i+=1。在 for 循环中 i 自动增加。

def create_bool():
    x_bool =[True, True, False, False, False, False]
    arr_bool = []
    for i in range(3):
        arr_bool.append(x_bool)
        x_bool = x_bool[-2:] + x_bool[0:-2]
        #i+=1
    
    print(arr_bool)
    
create_bool()

输出:

[[True, True, False, False, False, False], [False, False, True, True, False, False], [False, False, False, False, True, True]]

推荐阅读