列表/矩阵未保存正确的值

发布于 2024-10-07 19:33:55 字数 614 浏览 1 评论 0原文

我收到的作业有一个奇怪的问题。我们应该实现一个矩阵类。嗯,这并不难,但 Python 就是不会按照我的指示去做。但我确信有一个解释。

问题是,在下面的代码中,我尝试将值(在列表中提供)保存到矩阵中。

class simplematrix:
    matrix = [[]]
    def __init__(self, * args):
       lm = args[0]
       ln = args[1]
       values = args[2]
       self.matrix = [[0]*ln]*lm

       for m in range(lm):
           for n in range(ln):
               self.matrix[m][n] = values[(m*ln)+n]

vals = [0,1,2,3,4,5]
a = simplematrix(2,3,vals)

当我尝试打印矩阵时,我期望得到 [[0,1,2],[3,4,5]],如果我在一张纸上手动运行它,我就会得到它。如果我从 Python 打印矩阵,我会得到 [[3,4,5],[3,4,5]] 。 谁能告诉我为什么Python会这样,或者我是否在某个地方犯了一些愚蠢的错误? :)

I have a weird problem with an assignment I got. We are supposed to implement a matrix class. Well, it's not that hard, but Python just won't do as I tell it to. But I'm sure there is an explanation.

The problem is that, in the following code, I try to save values (provided in a list) into a matrix.

class simplematrix:
    matrix = [[]]
    def __init__(self, * args):
       lm = args[0]
       ln = args[1]
       values = args[2]
       self.matrix = [[0]*ln]*lm

       for m in range(lm):
           for n in range(ln):
               self.matrix[m][n] = values[(m*ln)+n]

vals = [0,1,2,3,4,5]
a = simplematrix(2,3,vals)

When I try to print the matrix, I expect to get [[0,1,2],[3,4,5]], which I get if I run it by hand, on a piece of paper. If I print the matrix from Python I get [[3,4,5],[3,4,5]] instead.
Can anyone tell me why Python acts like this, or if I made some stupid mistake somewhere? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一页 2024-10-14 19:33:55

问题出在 [[0]*ln]*lm 中。结果由 lm 对同一列表的引用组成,因此当您修改一行时,所有行都会发生更改。

尝试:

self.matrix = [[0]*ln for i in xrange(lm)]

The problem is in [[0]*ln]*lm. The result consists of lm references to the same list, so when you modify one row, all rows appear to change.

Try:

self.matrix = [[0]*ln for i in xrange(lm)]
吃颗糖壮壮胆 2024-10-14 19:33:55

Tim 和 aix 的答案纠正了您的错误,但这一步甚至没有必要,您可以使用列表理解在一行中完成整个事情:

self.matrix = [[values[m*ln+n] for n in range(ln)] for m in range(lm)]

您也可以说:

vals = range(6)

与您已经拥有的相反。这会整理你的代码并使其更加Pythonic。

The answers by Tim and aix correct your mistake, but that step isn't even necessary, you can do the whole thing in one line using a list comprehension:

self.matrix = [[values[m*ln+n] for n in range(ln)] for m in range(lm)]

You can also say:

vals = range(6)

as opposed to what you already have. This tidies up your code and makes it more Pythonic.

青衫负雪 2024-10-14 19:33:55

问题是 self.matrix = [[0]*ln]*lm 不会给您一个 lm 单独子列表的列表,而是一个 列表lm 引用单个相同列表[[0]*ln]

尝试一下

self.matrix = [[0]*ln for i in range(lm)]

(如果您使用的是 Python 2,请改用 xrange(lm))。

The problem is that self.matrix = [[0]*ln]*lm doesn't give you a list of lm separate sublists, but a list of lm references to the single same list [[0]*ln].

Try

self.matrix = [[0]*ln for i in range(lm)]

(If you're on Python 2, use xrange(lm) instead).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文