且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python列表列表 - 访问单个元素

更新时间:2023-01-21 23:31:47

我以前发生过这种情况,令人沮丧。

I've had this happen before and it's frustrating.

你的问题是这一行代码,这不是你所想的:

Your problem is this line of code, which isn't doing what you think it is:

loc1 = [[0.0] * ncols] * nrows

[0.0] * ncols 创建一个单独的列表,通过引用传递以形成您的2D列表。

[0.0] * ncols creates a single list which is passed by reference to form your 2D list.

尝试这样:

loc1 = [[0.0 for y in range(ncols)] for x in range(nrows)]