English 中文(简体)
在一个地点试图改变时,清单中多个职位的价值变化
原标题:Value changing at multiple positions in a list of list when trying to change at one place

The following Code in python:

matrix = [[0]*3]*2
matrix[0][1] = 1

seeems to be changing the value at all the matrix[][1]th positions, matrix becomes [[0,1,0],[0,1,0]] instead of [[0,1,0],[0,0,0]].

这是否是变数的初始化问题,还是 p的违约行为。

同时,我如何在某个时候只改变一个价值。

问题回答

繁多的操作者*在座标上的工作有一些缺点。

More generally, there are differences on its effects when applied on immutable objects such integers or strings, and how those it has when applied on mutable objects like lists and dictionaries.

这可能澄清问题:

>>> l = [0] * 3  #a list of immutable integers
>>> l
[0, 0, 0]
>>> l[0] = 1
>>> l
[1, 0, 0]
>>> l = [[0]] * 3  #a list of mutable lists
>>> l
[[0], [0], [0]]
>>> l[0][0] = 1
>>> l
[[1], [1], [1]]

<EDIT (评论中的库多斯到@lazyr) 在上述两种情况下,<代码>*的操作者都编制了具有相同特性的物体清单(主题地址),因此,各阵列中的每个编号(和每个清单)实际上都是相同的物体,但不可更改的类型只能替换,因此,如果你试图将新数值分配给各立体,则你将实际替换整个物体,而清单中的情况并非如此。 沿用前例(铭记id 功能返回物体的记忆地址:

>>> m = [1] * 3
>>> id(m[0])
39356344
>>> id(m[1])
39356344
>>> m[1] = 2
>>> id(m[1])
39356320  # new memory addres = different object!
>>> m = [[1]] * 3
>>> id(m[0])
40275408
>>> id(m[1])
40275408
>>> m[1][0] = 2
>>> id(m[1])
40275408  # same memory address = still the same object!

因此,在你看来,可能的工作是启动这样的矩阵:

>>> matrix = [[0 for i in range(3)] for j in range(2)]
>>> matrix
[[0, 0, 0], [0, 0, 0]]
>>> matrix[0][2] = 1
>>> matrix
[[0, 1, 0], [0, 0, 0]]

另一种更为激进的办法是完全转向numpy,其速度较快,从地面推向极快的矩阵和多层面病媒操纵,但使用也比较困难。

HTH!

Try:

matrix = [[0]*3 for j in xrange(2)]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签