理解实例化布尔2D数组? [英] Comprehension to instantiate a boolean 2D array?

查看:81
本文介绍了理解实例化布尔2D数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,所以这个问题,

I'm new to Python, thus the question,

因此,我试图实例化2D数组/列表,其中所有false的大小均为str +1行和pattern +1 cols.

So I'm trying to instantiate a 2D array/list with all false of the size str +1 rows and pattern +1 cols.

这是我的代码,

memo = []

for i in range(0, len(str) + 1):
    memo[i] = [[False] for j in range(len(pattern) + 1)]

现在我有两个问题,

在1行中是否有更多的pythonic方式做到这一点? 另外,如果我只是创建列表并且不对其进行任何初始化,那么每个网格中都有什么(java中非初始化的含义是用false表示的非初始化)?

Is there a more pythonic way of doing this in 1 line? Also if I just create the list and dont initialize it with anything, what is there in each grid(java equivalent of non-initialization meaning initalized with false)?

推荐答案

单线将是
memo = [[False for j in range(len(pattern) + 1)] for i in range(len(str) + 1)].

请注意,应避免使用str作为变量名,因为它会掩盖内置的str类型.

As a side-note, keep in mind that using str as a variable name should be avoided as it shadows the built-in str type.

如果我只是创建列表并且不使用任何内容对其进行初始化,那么每个网格中都有什么(java中非初始化的含义是用false表示的非初始化)?

if I just create the list and dont initialize it with anything, what is there in each grid(java equivalent of non-initialization meaning initalized with false)?

什么都没有,只是空着.

Nothing, it is simply empty.

Python列表存储对其他对象的引用.如果您不插入对列表的任何引用,则该列表不包含任何内容.

Python lists store references to other objects. If you don't insert any reference to the list, the list doesn't contain anything.

这篇关于理解实例化布尔2D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆