python - 创建布尔列表的列表
问题描述
我需要得到以下信息:([[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 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]]
推荐阅读
- python - .pop 从列表中删除项目会导致 python 中的变量范围出现奇怪的问题
- css - CSS 属性 background-image 支持的所有文件类型是什么?
- python - 如何在 Python 中更高效地分配以及它在内部是如何工作的?
- java - Selenium 正在读取占位符值
- python - 如何继续打印烂番茄中的所有前 100 部电影?
- jmeter - 有人使用 Sonarqube 来查看 loadrunner 脚本吗?
- ajax - 在 ASP.NET MVC 发布配置中未找到 ajax 错误
- laravel - 从 Laravel 中的数据库获取后如何“json_decode”单列数据?
- c# - EFcore 禁用自动向迁移添加属性
- git - 从分支中删除特定提交