I have 2d array,dimension 3x10,and I want to sort by values in 2nd row,from lowest to highest value。
解决方案
soi ' llassumethatby ' 2d阵列' youmeanalistoflists,such as:
lol=[范围(10 )、范围(12 )、范围(15 ) ]
or the like -- i.e. a list with 3 items,each item being a list with 10 items,and the ' second row ' wouldbethesublistitemlol [1]. butyourquestionissomaddeninglyvaguethatthere ' snowaytoavoidmakingassumptions-edityourqtoclarifywithmore、ifyoudislikepepeopoplptiore
soundertheseassumptionsyoucansorteachofthe3sublistsintheorderrequiredtosortthesecondone,for example:
索引=范围(10 )。
indices.sort (key=lol [1]._ _ getitem _ ) )
for i,sublistinenumerate(lol ) :
thegeneralapproachereistosorttherangeofindices,thenjustusethatappropriatelysortedrangetoreorderallthesubliststsinplay。
ifyouactuallyhaveadifferentproblem,therewillofcoursebedifferentsolutions; - )。