简体   繁体   中英

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? 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.

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. 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. then lets convert this new_list to array as follow:

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. 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 :

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

All the values are initialized to 0.

You can easily update values in 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)']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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