【问题标题】:create list of Boolean's list创建布尔列表的列表
【发布时间】:2021-03-12 13:00:43
【问题描述】:

我需要得到以下信息:([[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]])

【问题讨论】:

  • 您的轮播略有偏差:x_bool = x_bool[-2:] + x_bool[:-2]
  • 为什么不只是 arr_bool = [[True, True, False, False, False, False],[ False, False,True, True, False, False],[False, False, False, False ,对,对,]]?
  • 这是一个简单的例子(真实案例最多可能包含10000个真/假)

标签: python loops boolean sequence boolean-operations


【解决方案1】:

您需要删除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]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2021-03-05
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 2014-05-16
    相关资源
    最近更新 更多