Python - 动态嵌套列表 [英] Python - Dynamic Nested List

查看:25
本文介绍了Python - 动态嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图根据宽度和高度在 Python 中生成一个嵌套列表.这是我目前所拥有的:

 宽度 = 4高度 = 5行 = [无]*宽度地图 = [行]*高度

现在,这显然不太正确.打印后看起来不错:

[[无,无,无,无],[无,无,无,无],[无,无,无,无],[无,无,无,无],[无,无,无,无]]

但是试图像这样为一个位置赋值:

map[2][3] = 'foo'

我明白了:

[[None, None, None, 'foo'],[无,无,无,'富'],[无,无,无,'富'],[无,无,无,'富'],[无,无,无,'foo']]

很明显,这是因为每个子列表实际上只是引用相同的对象,行,因此更改一个,将更改它们.所以这是我最接近的!

如何动态生成嵌套列表?谢谢!

解决方案

当您执行 [row]*height 时,您最终会在每一行中得到相同的列表对象.row 数组引用在每一行中重复,这意味着每一行实际上都指向同一个列表对象.因此,修改一行实际上会修改所有行.

看看当你打印 id() 每行.他们都一样!

<预><代码>>>>网格 = [[无] * 宽度] * 高度>>>[网格中行的 id(row)][148014860、148014860、148014860、148014860、148014860]

您可以使用列表推导让 python 为每一行生成单独但相同的列表.当您使用 [rowexpr for i in xrange(height)] 时,rowexpr 将每行计算一次.诀窍是使用一个表达式,每次求值时都会产生一个唯一的列表.

如果你看到它在行动,这会更有意义:

<预><代码>>>>网格 = [[无] * x 范围内 i 的宽度(高度)]>>>网格[2][3] = 'foo'>>>网格[[无,无,无,无],[无,无,无,无],[无,无,无,'富'],[无,无,无,无],[无,无,无,无]]

每次对 [None] * width 求值时,它都会生成一个新列表.

<预><代码>>>>[网格中行的 id(row)][148016172、148015212、148016236、148016108、148016332]

So I'm trying to generate a nested list in Python based on a width and a height. This is what I have so far:

    width = 4
    height = 5
    row = [None]*width
    map = [row]*height

Now, this obviously isn't quite right. When printed it looks fine:

[[None, None, None, None],
 [None, None, None, None],
 [None, None, None, None],
 [None, None, None, None],
 [None, None, None, None]]

But attempting to assign a value to a position like so:

map[2][3] = 'foo'

I get:

[[None, None, None, 'foo'],
 [None, None, None, 'foo'],
 [None, None, None, 'foo'],
 [None, None, None, 'foo'],
 [None, None, None, 'foo']]

Clearly this happens because each sublist is really just referencing the same object, row, so changing one, changes them all. So this is closest I've got!

How can I dynamically generate a nested list? Thanks!

解决方案

When you do [row]*height you end up with the same list object in each row. The row array reference is repeated in each row which means each row is actually pointing to the same list object. Hence modifying one row actually modifies all rows.

Take a look at what happens when you print the id() for each row. They're all the same!

>>> grid = [[None] * width] * height
>>> [id(row) for row in grid]
[148014860, 148014860, 148014860, 148014860, 148014860]

You can get python to generate separate-but-identical lists for each row by using a list comprehension. When you use [rowexpr for i in xrange(height)] then rowexpr will be evaluated once per row. The trick then is to use an expression that will result in a unique list each time it is evaluated.

This'll make more sense if you see it in action:

>>> grid = [[None] * width for i in xrange(height)]
>>> grid[2][3] = 'foo'
>>> grid
[[None, None, None, None],
 [None, None, None, None],
 [None, None, None, 'foo'],
 [None, None, None, None],
 [None, None, None, None]]

Each time [None] * width is evaluated it generates a new list.

>>> [id(row) for row in grid]
[148016172, 148015212, 148016236, 148016108, 148016332]

这篇关于Python - 动态嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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