ProblemaHai dati in uno o più elenchi o vettori di matrici che devi ordinare.
SoluzioneUso Ordina elenco() o Ordina elenco in() , Rango() oppure inserisci gli elenchi e i vettori in una tabella di dati e ordinali.
// Sort List example (makes new list)
names = {"fred", "Andy", "Rory", "bess", "susy", "perl"};
sname = Sort List( names );
Show( names, sname );nomi = {"fred", "Andy", "Rory", "bess", "susy", "perl"}; // invariato
sname = {"Andy", "bess", "fred", "perl", "Rory", "susy"}; // smistato // Sort List Into example (in-place sort)
names = {"fred", "Andy", "Rory", "bess", "susy", "perl"};
Sort List Into( names );
Show( names );nomi = {"Andy", "bess", "fred", "perl", "Rory", "susy"}; // ordinato sul postoEntrambi gli esempi precedenti operano su un unico elenco. Se c'è un secondo elenco di numeri di telefono, non si limitano ai nomi.
Per fare in modo che diverse liste abbiano tutte lo stesso ordinamento, puoi usare la funzione Rank(). Gli elenchi devono avere lo stesso numero di elementi e nessun valore mancante nell'elenco delle chiavi di ordinamento.
names = {"fred", "Andy", "Rory", "bess", "susy", "perl"};
phone = {"x321", "x322", "x323", "x324", "x325", "x326"};
rooms = {"1003", "1001", "1005", "1002", "1006", "1004"};
rnk = Rank( names ); // [2, 4, 1, 6, 3, 5]
// use the rnk vector to make sorted lists from the old lists
snames = names[rnk];
sphone = phone[rnk];
srooms = rooms[rnk];
Show( snames, sphone, srooms );snames = {"Andy", "bess", "fred", "perl", "Rory", "susy"}; telefono = {"x322", "x324", "x321", "x326", "x323", "x325"}; sroom = {"1001", "1002", "1003", "1004", "1005", "1006"};
Sopra, i nuovi elenchi con il prefisso s sono tutti ordinati per nome.
Se hai più elenchi, della stessa lunghezza, ma accoppiati tra gli elenchi, forse una tabella di dati sarebbe una struttura di dati migliore. Potresti trovare più facile usare una tabella perché puoi vederla, salvarla, sottoimpostarla, ...Ma se hai già le liste, puoi costruirne una tabella. Ecco una funzione utente riutilizzabile che lo fa:
// the zip function, named after a Python function that, like a zipper, zips two lists together
zip = Function( {variableNameList}, // a list of variable names
{iVarName, dt, data}, // local variables
dt = New Table( "zip", "invisible" ); // a hidden table, returned at end of this function
For( iVarName = 1, iVarName <= N Items( variableNameList ), iVarName += 1,
data = Eval( variableNameList[iVarName] ); // makes a copy of the data from the name
If( Is Matrix( data ) | Is Number( data[1] ), // handle lists of numbers same as a matrix
dt << New Column( Char( variableNameList[iVarName] ), "numeric", values( data ),
setProperty( "wasMatrix", eval(Is Matrix( data ) ) ) ) // remember how to convert back, could be list
, // else a list of strings
dt << New Column( Char( variableNameList[iVarName] ), "character", values( data ),
setProperty( "wasString", eval( Is String( data[1] ) ) ) ) // remember how to convert back, could be names
);
);
dt << New Column( "_original order_", values( 1 :: N Rows( dt ) ) ); // this extra column is not required, but might be useful
dt << setdirty( 0 ); // not required but avoids a "save?" prompt
dt; // return
);esempio:
cars = {chevy, volkswagon, honda, ford, toyota}; // list of unquoted names
doors = [4, 2, 2, 5, 2]; // matrix of numbers
miles = {30000, 20000, 40000, 10000, 50000}; // list of numbers
owner = {"fred", "susan", "ralph", "penelope", "terry"}; // list of quoted strings
zippedDt = zip( {cars, doors, miles, owner} );
zippedDt << sort( by( miles ), replacetable( 1 ) );
zippedDt << show Window; // table hidden by default
Lists are in table's columns, sorted by milesGli elenchi sono nelle colonne della tabella, ordinati per migliaSe stai lavorando con gli elenchi, potresti desiderare di nuovo tutti gli elenchi. Ecco una funzione di decompressione:
// unzip, similar to Python's unzip
unzip = Function( {dt}, // data table previously returned from zip (above)
{result, cols, iCol, col, names, iRow}, // local variables
cols = dt << getcolumnnames; // below, the _original order_ col gets skipped
result = {}; // assemble all of the lists and matrices here, return at bottom
For( iCol = 1, iCol < N Items( cols )/*skip last one*/, iCol += 1,
col = Column( dt, cols[icol] ); // the Ith col in the table
If( col << getdatatype( "English" ) == "Numeric", //
// return the same type of object, list or matrix, supplied originally
If( col << getproperty( "wasMatrix" ), // by checking the property we set above
Insert Into( result, col << getValues ); // append a matrix
, // else append a list of numbers
result[N Items( result ) + 1] = As List( dt[0, iCol] )
)
, // else character column
// return same type, list of names or list of strings...
If( col << getproperty( "wasString" ), // check character col prop set above
result[N Items( result ) + 1] = (dt[0, iCol]) // list of strings
, // else turn the strings into names again
result[N Items( result ) + 1] = As Name( dt[0, iCol] ) // list of names
)
);
);
result; // return
);esempio:
{cars, doors, miles, owner} = unzip( zippedDt );Lista( 4 elementi ) assegnata.
automobili:{ford, volkswagon, chevy, honda, toyota}
porte:[5, 2, 4, 2, 2]
miglia:{10000, 20000, 30000, 40000, 50000}
proprietario:{"penelope", "susan", "fred", "ralph", "terry"}
Infine
// Important! delete the invisible table!
Close( zippedDt, nosave );
DiscussioneAssicurati di ripulire la tabella invisibile se usi le funzioni zip/unzip. (Non sono correlati alla funzione di compressione per i file .zip. JMP ha il supporto per questo nell'oggetto archivio zip.)
La tabella dati è un ottimo strumento per strutture dati complicate. Puoi creare tabelle nascoste o private per la gestione dei dati senza utilizzare le funzioni wrapper di cui sopra. Gli elenchi possono essere più efficienti delle tabelle di dati se è necessario aggiungere e rimuovere molti elementi (le code e gli stack potrebbero essere lenti se implementati come aggiunta e rimozione di righe da una tabella). La funzione zip (sopra) prevede un elenco di nomi . UN nome (senza virgolette) non equivale a a stringa (utilizza le virgolette) . Il passaggio di un elenco di nomi riduce il numero di volte in cui i dati vengono copiati.
Guarda anchehttps://www.jmp.com/support/help/14-2/list-functions.shtml