简体   繁体   English

将列表转换为 Python 中的矩阵

[英]converting list into a matrix in Python

If you have a list of elements lets say:如果您有一个元素列表,可以说:

res = 
['(18,430)', '(19,430)', '(19,429)', '(19,428)', '(19,427)', '(18,426)', '(17,426)', '(17,425)', '(17,424)', '(17,423)', '(17,422)', '(17,421)', '(17,420)', '(16,421)', '(14,420)', '(11,419)', '(9,417)', '(7,416)', '(4,414)', '(3,414)', '(2,412)', '(1,412)', '(-1,410)', '(-2,409)', '(-2,408)', '(-3,407)', '(-3,406)', '(-3,405)', '(-3,404)', '(-3,403)', '(-3,402)', '(-3,401)', '(-3,400)', '(-4,399)', '(-4,398)', '(-5,398)', '(-6,398)', '(-7,397)', '(-7,396)', '(-6,395)', '(-5,395)', '(-4,393)', '(-3,391)', '(6,384)', '(12,378)', '(24,370)', '(42,358)', '(107,304)', '(151,255)', '(207,196)', '(259,121)', '(389,-28)', '(456,-84)', '(515,-134)', '(569,-182)', '(650,-260)', '(688,-294)', '(723,-317)', '(740,-328)', '(762,-342)', '(767,-347)', '(768,-349)', '(769,-352)', '(769,-357)', '(769,-359)', '(768,-361)', '(768,-364)', '(766,-370)', '(765,-371)', '(764,-374)', '(763,-376)', '(761,-378)', '(760,-381)', '(758,-385)', '(752,-394)', '(747,-401)', '(742,-407)', '(735,-413)', '(724,-421)', '(719,-424)', '(718,-425)', '(717,-425)'], ['(18,430)', '(19,430)', '(19,429)', '(19,428)', '(19,427)', '(18,426)', '(17,426)', '(17,425)', '(17,424)', '(17,423)', '(17,422)', '(17,421)', '(17,420)', '(16,421)', '(14,420)', '(11,419)', '(9,417)', '(7,416)', '(4,414)', '(3,414)', '(2,412)', '(1,412)', '(-1,410)', '(-2,409)', '(-2,408)', '(-3,407)', '(-3,406)', '(-3,405)', '(-3,404)', '(-3,403)', '(-3,402)', '(-3,401)', '(-3,400)', '(-4,399)', '(-4,398)', '(-5,398)', '(-6,398)', '(-7,397)', '(-7,396)', '(-6,395)', '(-5,395)', '(-4,393)', '(-3,391)', '(6,384)', '(12,378)', '(24,370)', '(42,358)', '(107,304)', '(151,255)', '(207,196)', '(259,121)', '(389,-28)', '(456,-84)', '(515,-134)', '(569,-182)', '(650,-260)', '(688,-294)', '(723,-317)', '(740,-328)', '(762,-342)', '(767,-347)', '(768,-349)', '(769,-352)', '(769,-357)', '(769,-359)', '(768,-361)', '(768,-364)', '(766,-370)', '(765,-371)', '(764,-374)', '(763,-376)', '(761,-378)', '(760,-381)', '(758,-385)', '(752,-394)', '(747,-401)', '(742,-407)', '(735,-413)', '(724,-421)', '(719,-424)', '(718,-425)', '(717,-425)']

and we want to make all these values into a matrix where we can update values.我们希望将所有这些值变成一个矩阵,我们可以在其中更新值。 All these values in the list are going to be the values of the rows and columns of a matrix?列表中的所有这些值都将是矩阵的行和列的值吗? Basically:基本上:

row1 = '(18,430)', row2 = '(19,430)', row3 = '(19,429)',.....,rown='(717,-425)', column1 = '(18,430)', column2 = '(19,430)', column3 = '(19,429)', ..... ,columnn= '(717,-425)'

How can we do that in Python and later I want to update values in the rows and columns?我们如何在 Python 中做到这一点,以后我想更新行和列中的值? I tried to do this where I repeat the list and make it into a matrix.我尝试在重复列表并将其制成矩阵的地方执行此操作。 But it does not give me what I want.但它并没有给我想要的东西。

Res_List = [res,res]
print(np.array(Res_List))

So I am still wondering how we can do this in Python.所以我仍然想知道我们如何在 Python 中做到这一点。

I also tried:我也试过:

mat = np.array([res,res]).T
print(mat)

and it kind of gives me what I want but not quite.它给了我我想要的东西,但并不完全。 This gives me:这给了我:

[['(18,430)' '(18,430)']
 ['(19,430)' '(19,430)']
 ['(19,429)' '(19,429)']
 ['(19,428)' '(19,428)']
 ['(19,427)' '(19,427)']
 ['(18,426)' '(18,426)']
 ['(17,426)' '(17,426)']
 ['(17,425)' '(17,425)']
 ['(17,424)' '(17,424)']
 ['(17,423)' '(17,423)']
 ['(17,422)' '(17,422)']
 ['(17,421)' '(17,421)']
 ['(17,420)' '(17,420)']
 ['(16,421)' '(16,421)']
 ['(14,420)' '(14,420)']
 ['(11,419)' '(11,419)']
 ['(9,417)' '(9,417)']
 ['(7,416)' '(7,416)']
 ['(4,414)' '(4,414)']
 ['(3,414)' '(3,414)']
 ['(2,412)' '(2,412)']
 ['(1,412)' '(1,412)']
 ['(-1,410)' '(-1,410)']
 ['(-2,409)' '(-2,409)']
 ['(-2,408)' '(-2,408)']
 ['(-3,407)' '(-3,407)']
 ['(-3,406)' '(-3,406)']
 ['(-3,405)' '(-3,405)']
 ['(-3,404)' '(-3,404)']
 ['(-3,403)' '(-3,403)']
 ['(-3,402)' '(-3,402)']
 ['(-3,401)' '(-3,401)']
 ['(-3,400)' '(-3,400)']
 ['(-4,399)' '(-4,399)']
 ['(-4,398)' '(-4,398)']
 ['(-5,398)' '(-5,398)']
 ['(-6,398)' '(-6,398)']
 ['(-7,397)' '(-7,397)']
 ['(-7,396)' '(-7,396)']
 ['(-6,395)' '(-6,395)']
 ['(-5,395)' '(-5,395)']
 ['(-4,393)' '(-4,393)']
 ['(-3,391)' '(-3,391)']
 ['(6,384)' '(6,384)']
 ['(12,378)' '(12,378)']
 ['(24,370)' '(24,370)']
 ['(42,358)' '(42,358)']
 ['(107,304)' '(107,304)']
 ['(151,255)' '(151,255)']
 ['(207,196)' '(207,196)']
 ['(259,121)' '(259,121)']
 ['(389,-28)' '(389,-28)']
 ['(456,-84)' '(456,-84)']
 ['(515,-134)' '(515,-134)']
 ['(569,-182)' '(569,-182)']
 ['(650,-260)' '(650,-260)']
 ['(688,-294)' '(688,-294)']
 ['(723,-317)' '(723,-317)']
 ['(740,-328)' '(740,-328)']
 ['(762,-342)' '(762,-342)']
 ['(767,-347)' '(767,-347)']
 ['(768,-349)' '(768,-349)']
 ['(769,-352)' '(769,-352)']
 ['(769,-357)' '(769,-357)']
 ['(769,-359)' '(769,-359)']
 ['(768,-361)' '(768,-361)']
 ['(768,-364)' '(768,-364)']
 ['(766,-370)' '(766,-370)']
 ['(765,-371)' '(765,-371)']
 ['(764,-374)' '(764,-374)']
 ['(763,-376)' '(763,-376)']
 ['(761,-378)' '(761,-378)']
 ['(760,-381)' '(760,-381)']
 ['(758,-385)' '(758,-385)']
 ['(752,-394)' '(752,-394)']
 ['(747,-401)' '(747,-401)']
 ['(742,-407)' '(742,-407)']
 ['(735,-413)' '(735,-413)']
 ['(724,-421)' '(724,-421)']
 ['(719,-424)' '(719,-424)']
 ['(718,-425)' '(718,-425)']
 ['(717,-425)' '(717,-425)']]

but what I want is the columns like how they are designed but the rows to be the same as the columns and that we are able to update and put values into the matrix.但我想要的是列就像它们的设计方式一样,但行与列相同,并且我们能够更新并将值放入矩阵中。

you can use Numpy.您可以使用 Numpy。 for converting a list to a matrix like array you should write it as list of lists (or tuples).要将列表转换为类似数组的矩阵,您应该将其写为列表(或元组)列表。 First of all your list contain strings so we first convert strings to tuples as follow:首先,您的列表包含字符串,因此我们首先将字符串转换为元组,如下所示:

new_list = [eval(i) for i in res]

I used eval because your strings is in tuple form so we can tell python treat them as a chunk of code.我使用 eval 是因为您的字符串是元组形式,所以我们可以告诉 python 将它们视为一段代码。 then lets convert this new_list to array as follow:然后让我们将此 new_list 转换为数组,如下所示:

import numpy as np
matrix = np.array(new_list )

now you can access your matrix elements as matrix[i, j] where i, j are row and column respectively.现在您可以将矩阵元素作为 matrix[i, j] 访问,其中 i, j 分别是行和列。 for changing a specific value of in certain location just assign it as usual:要更改特定位置的特定值,只需像往常一样分配它:

matrix[i, j] = new_value

Maybe what you want is a dict :也许你想要的是一个dict

matrix = {
    k: {l: 0 for l in res}
    for k in res
}

All the values are initialized to 0.所有值都初始化为 0。

You can easily update values in matrix ;您可以轻松更新matrix中的值; for example, you can increase the value of a 'cell' of one:例如,您可以将“单元格”的值增加一:

matrix['(18,430)']['(19,430)'] += 1

or set it to a specific value:或将其设置为特定值:

matrix['(18,430)']['(19,430)'] = 10

and retrieve it:并检索它:

val = matrix['(18,430)']['(19,430)']

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

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