简体   繁体   English

在Python中有什么方法可以追加?

[英]In Python is there any way to append behind?

I have a matrix: 我有一个矩阵:

[[1 2 3],
[4 5 6], 
[7 8 9]]  

and I need to create a new matrix: 我需要创建一个新矩阵:

[[7 4 1],
[8 5 2],
[9 6 3]]

I tried 我试过了

new_matrix = [[1]]
new_matrix.append(matrix[1][0])

and got a new_matrix = [4 1] instead of a new_matrix = [1 4] 得到一个new_matrix = [4 1]而不是new_matrix = [1 4]

If you need more clarification, please just ask. 如果您需要更多说明,请询问。

Yes. 是。 Use new_matrix.insert(0,matrix[1][0]) . 使用new_matrix.insert(0,matrix[1][0])

insert(position,value) allows you to insert objects into specified positions in a list. insert(position,value)允许您将对象插入列表中的指定位置。 In this case, since you want to insert a number at the beginning, the position is zero. 在这种情况下,由于您要在开头插入数字,因此位置为零。

Note, however, that this take O(n) time if new_matrix has n elements. 但是请注意,如果new_matrix有n个元素,则需要O(n)时间。 If new_matrix has 100 elements, it will take ten times longer to add something to the beginning than if it has 10. That's much slower than adding something to the end of the list, which usually takes O(1): it should be fast regardless of how big new_matrix is. 如果new_matrix有100个元素,那么在开头添加一些元素需要花费十倍的时间比它有10个元素。这比在列表末尾添加一些东西要慢得多,这通常需要O(1):它应该是快速的new_matrix有多大。 See here for more on time complexity of python operations. 有关python操作的时间复杂性的更多信息,请参见此处 If you'll regularly be adding elements to the beginning of lists, you might want to think about whether you can reverse what you're doing. 如果您经常在列表的开头添加元素,则可能需要考虑是否可以撤消正在执行的操作。

Also, note that the way you've done things, this will give you a new_matrix of [4,[1]]. 另外,请注意你做事的方式,这将给你一个[4,[1]]的new_matrix。 I'm not quite sure what you want: if you want the final results as you are describing them, then you need new_matrix = [1] . 我不太确定你想要什么:如果你想要最终结果,那么你需要new_matrix = [1] If your code is correct ( new_matrix = [[1]] ), and you want [[4,1]] , then you'll need to do new_matrix[0].insert(0,4) . 如果你的代码是正确的( new_matrix = [[1]] ),并且你想要[[4,1]] ,那么你需要做new_matrix[0].insert(0,4) If you want [[4],[1]] , you'll need to do new_matrix.insert(0,[4]) , and so on. 如果你想要[[4],[1]] ,你需要做new_matrix.insert(0,[4]) ,依此类推。

As an aside, since you seem to be doing things with matrices, have you considered using numpy? 顺便说一句,既然你似乎在用矩阵做事,你考虑过使用numpy吗?


(I'd like to point out that, if this answer seems a bit off-topic, it's because this question was edited to be something entirely different than was originally asked.) (我想指出的是,如果这个答案看起来有点偏离主题,那是因为这个问题被编辑成与最初提出的问题完全不同。)

As for the new question: while Stefan's answer is good python, you may be giving yourself too much work. 至于新问题:虽然Stefan的答案很好,但你可能会给自己太多的工作。 It's clear you're trying to implement something like a matrix transpose, except mirrored. 很明显,你试图实现类似矩阵转置的东西,除了镜像。 If you're doing those sorts of manipulations, Numpy is much easier and faster. 如果你正在做那些操作, Numpy容易,更快。 In this case, with numpy arrays, you'd just need to do the following: 在这种情况下,使用numpy数组,您只需要执行以下操作:

import numpy as np # import numpy
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Here's your matrix
new_matrix_1 = matrix[::-1,:] # Here's your matrix with all the rows reversed: [[7,8,9],[4,5,6],[1,2,3]]
new_matrix = new_matrix_1.T # Here's the transpose of that, which is what you want.

While this is just one thing, this will make everything you do easier. 虽然这只是一件事,但这会让你做的一切变得更轻松。 For example, arithmetic will actually work: new_matrix+matrix , 2*new_matrix , and so on. 例如,算术实际上可以工作: new_matrix+matrix2*new_matrix等等。 You'd have to implement this manually otherwise. 您必须手动实现此操作。

Why build the matrix in tiny steps when you can just do it at once? 为什么当你可以立刻完成时,为什么要以微小的步骤构建矩阵?

>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> new_matrix = zip(*matrix[::-1])
>>> new_matrix
[(7, 4, 1), (8, 5, 2), (9, 6, 3)]

Or if you do need the rows as lists: 或者,如果您确实需要行作为列表:

>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> new_matrix = map(list, zip(*matrix[::-1]))
>>> new_matrix
[[7, 4, 1], [8, 5, 2], [9, 6, 3]]

Explanation: 说明:

  • First the [::-1] reverses the matrix, ie, turns it upside down. 首先, [::-1]反转矩阵,即将其颠倒。 That gets us a step closer to the desired result. 这让我们更接近理想的结果。
  • The zip then transposes the matrix by combining its rows. 然后, zip通过组合其行来转置矩阵。 First element of the zip result is the list of the first element of each row, ie, first element of the zip is the first column . zip结果的第一个元素是每行的第一个元素的列表,即zip的第一个元素是第一 And so on, ie, the zip gives you a list of the columns, ie, the transposed matrix. 等等,即zip会为您提供列的列表,即转置矩阵。

From: 从:

[[1 2 3],
[4 5 6], 
[7 8 9]] 

to

[[7 4 1],
[8 5 2],
[9 6 3]]

can be done as 可以做到

import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix[::-1].T

If you're into list comprehensions and stuff: 如果你进入列表理解和东西:

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
result = [ [ i[k] for i in matrix[::-1] ] for k in range(len(matrix)) ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM