Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...